skip to Main Content

I’m converting html to pdf using itextPDF, everything works as expected but for one of the GIF image after conversion to pdf only the skeleton of the image appears. This is a static GIF image not animated just an image with transparent background and image in center. when I run the code through other GIF images with transparent background works as expected. I’m using the below code to convert HTML to PDF. I have attached a sample image of the issue.sample image

    PdfWriter writer = new PdfWriter(inMemoryStream);
    PdfDocument pdf = new PdfDocument(writer);
    PdfMerger merger = new PdfMerger(pdf);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument temp = new PdfDocument(new PdfWriter(baos));
    HtmlConverter.convertToPdf(test, temp, converterProperties);
    temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
    merger.merge(temp, 1, temp.getNumberOfPages());
    temp.close();
    pdf.close();
    inMemoryStream.close();

2

Answers


  1. import com.itextpdf.html2pdf.ConverterProperties;
    import com.itextpdf.html2pdf.HtmlConverter;
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfMerger;
    import com.itextpdf.kernel.pdf.PdfWriter;
    
        
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    public class HtmlToPdfConverter {
    
        public static void main(String[] args) {
            // Replace "test" with your HTML content as a String
            String htmlContent = "<html><body><p>Your HTML content here</p></body></html>";
    
            try {
                ByteArrayOutputStream inMemoryStream = new ByteArrayOutputStream();
                PdfWriter writer = new PdfWriter(inMemoryStream);
                PdfDocument pdf = new PdfDocument(writer);
                PdfMerger merger = new PdfMerger(pdf);
                ConverterProperties converterProperties = new ConverterProperties();
    
                // Add your custom properties if needed
                // converterProperties.set...
    
                // Convert HTML to PDF
                InputStream htmlInputStream = new ByteArrayInputStream(htmlContent.getBytes());
                HtmlConverter.convertToPdf(htmlInputStream, pdf, converterProperties);
    
                // Close the PDF and perform further operations
                pdf.close();
    
                // Now you can work with the generated PDF in 'inMemoryStream'
                // ...
    
                inMemoryStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    Login or Signup to reply.
  2. This should not be a problem if you understand how PDF handles transparency.

    enter image description here

    It does not matter if an Image is TIFF PNG or GIF with transparency they are all stored in a similar fashion as 2 images. One is the opaque DeviceRGB the other is the Alpha channel as a black "SoftMask" we can see one image in left column, and 2 images mentioned in right column, and the 2 images (RGB overlaid with SoftMask) in the central PDF view.

    The issue here is bad image generation as the SoftMask has not been drawn well enter image description here

    it should look like this
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search