Wednesday, April 16, 2014

Rotativa corrupting or "ghosting" images when converting a JPEG in HTML to aPDF document

I'm using Rotativa to generate PDFs from Html Razor views.


v1.5.0 display images correctly, but doesn't paginate correctly
v1.6.1 correctly paginates from the html, but "ghosts" the images  Per this bug:


this is because of wkhtmltopdf which has a bug... it's supposed to be fixed in the trunk, but it's not in the versions I downloaded from their page.




The workaround is to convert the JPEG to one of the other "not jacked up" image formats..


In our case we converted to PNG. There is some loss of clarity, but we can live with that.






try

{
  byte [] byteArrayIn = ( byte[] )@Model.ETA640StudentProfileVM[ currentRecord ].ImageObj;

  byte[] byteArrayOut = null;




 MemoryStream ms = new MemoryStream( byteArrayIn, 0, byteArrayIn.Length );

  ms.Write( byteArrayIn, 0, byteArrayIn.Length );

  Image returnImage = Image.FromStream( ms, true );

  using (var output = new MemoryStream())


  {
    returnImage.Save( output, System.Drawing.Imaging.ImageFormat.Png );

    byteArrayOut = output.ToArray();


  };

  @:<img src="data:image/png;base64,@(Html.Raw( Convert.ToBase64String( byteArrayOut )))" alt="Image Not Available" height="155" />

}
catch

{
  @:<img src="" alt="Error Generating Image" height="155" />


}

No comments:

Post a Comment