2

I have 3 htmls, header, body and footer. I use XMLWorker to transform html and Base64ImageProvider for the images. But i can´t use that provider in my header. How can I transform the header html to show the images? I need help. My code:

public class PdfUtils {
public static void createPdf(String header, String body, String footer) throws IOException, DocumentException {
        String dir = "C:/";
        String outputFile = "letter.pdf";

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dir + outputFile));
        document.open();
        writer.setPageEvent(new PDFHeaderFooter(header, footer));

        CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        htmlContext.setImageProvider(new Base64ImageProvider());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new ByteArrayInputStream(body.getBytes()));

        // step 5
        document.close();
    }
}

public class PDFHeaderFooter extends PdfPageEventHelper {
    protected ElementList header;
    protected ElementList footer;

    public PDFHeaderFooter(String headerHtml, String footerHtml) throws IOException {
        header = XMLWorkerHelper.parseToElementList(headerHtml, null);
        footer = XMLWorkerHelper.parseToElementList(footerHtml, null);
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            PdfContentByte canvas = writer.getDirectContent();
            ColumnText ct = new ColumnText(canvas);
            ct.setSimpleColumn(36, 832, 559, 810);
            for (Element e : header) {
                ct.addElement(e);
            }
            ct.go();
            ct.setSimpleColumn(36, 10, 559, 32);
            for (Element e : footer) {
                ct.addElement(e);
            }
            ct.go();
        }
        catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}

Thanks!

IMarco
  • 21
  • 1
  • This functionality is provided out-of-the-box in the most recent version of iText's HTML to PDF conversion (iText 7 + HTML). If you are using an older version (iText suspect iText 5 and XML Worker), you should read the answer to this question: [HTML to PDF with base64 images throws FileNotFoundException](http://stackoverflow.com/questions/29194405/html-to-pdf-with-base64-image-throw-filenotfound) – Bruno Lowagie Aug 22 '17 at 09:56
  • Thanks for your reply. The project need JDK1.6, is possible use itext7 with java6? With itext5, is not possible add an html image in header? Thanks a lot. – IMarco Aug 23 '17 at 06:16
  • Jaba 6? No, iText 7 needs Java 7. Why would anyone still use Java 6? https://stackoverflow.com/questions/17671899 – Bruno Lowagie Aug 23 '17 at 06:20
  • I honestly do not know, it's customer requirement – IMarco Aug 23 '17 at 08:49
  • Maybe the customer doesn't care about security and maintenance. I hear that there are still companies using insecure MSIE 6 version as their browser. If I were you, I wouldn't work for such a customer. On another note: did the customer mention if he's willing to purchase a commercial iText license? Because he might not want to use iText under the AGPL (since that might mean that he has to publish the complete source code of his application as AGPL software). – Bruno Lowagie Aug 23 '17 at 09:15
  • I dont know. Thanks a lot for your replies. If i cant use base64 images (from html) in header with itext5, i will talk with the customer for find another solution. Regars! – IMarco Aug 23 '17 at 10:14
  • Er... You *can* use base64 images from HTML with iText 5, but you need to add some extra code. Maybe you didn't fully read my first comment. I will close your question as a duplicate. If you read the answer to the duplicate question you'll understand that your assumption is wrong. I never said it wasn't possible; I even pointed you at a solution! – Bruno Lowagie Aug 23 '17 at 10:19
  • The answer to the original question explains that you have to create a custom `ImageProvider`. Of course, it would be better for your customer to upgrade to a Java version that hasn't been declared dead, in which case, you can use iText 7, and this solution: [Can pdfHTML render Base64 images to PDF?](http://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/can-pdfhtml-render-base64-images-pdf) – Bruno Lowagie Aug 23 '17 at 10:22
  • Excuse me if i dont undertand you, my english is poor. I use a custom Base64 image provider and its ok in body part with XMLParser. To create a header, i dont know hoy use my Base64 image provider, because a cant use XMLParser, only XMLWorkerHelper to transform my html code, and i dont know how use my HtmlPipelineContext in header. I feel the confusion, sorry. – IMarco Aug 23 '17 at 10:57
  • `XMLWorkerHelper` uses `XMLParser` internally. Why wouldn't you be able to use `XMLParser` just because you create a header? That doesn't make sense to me. – Bruno Lowagie Aug 23 '17 at 11:15
  • In all examples to create a header from an html, use, for example: header = XMLWorkerHelper.parseToElementList(HEADER, null);... in pageEvent. Then, create a Rectangle to make the header. I dont know how use XMLParse and use my image provider in header. How can i use my ImageProvider in that example (http://developers.itextpdf.com/question/how-add-html-headers-and-footers-page)? Thanks and sorry, i´m a new user with pdf´s. – IMarco Aug 23 '17 at 11:39
  • Why don't you replace `XMLWorkerHelper.parseToElementList(HEADER, null);` with a call to a helper function that accepts a `String` as parameter, and that returns a `List`. In that helper method, let's call it `parseHeaderFooter()`, you put the full-blown `XMLParser` code. Are you saying you don't know how to do this? – Bruno Lowagie Aug 23 '17 at 11:58
  • Sorry, its possible. I dont see any method to get the elements in the class XmlParser or XmlWorker to return the List. Thanks again. – IMarco Aug 23 '17 at 12:26
  • Have you ever look at the API documentation? If so, can you guess what the [ElementHandlerPipeline](http://itextsupport.com/apidocs/itext5/5.5.11/com/itextpdf/tool/xml/pipeline/end/ElementHandlerPipeline.html) pipeline is for? If not, see my answer to this question: [How can I convert XHTML nested list to pdf with iText?](https://stackoverflow.com/questions/26755315/how-can-i-convert-xhtml-nested-list-to-pdf-with-itext) – Bruno Lowagie Aug 23 '17 at 13:09
  • I will check the documentation before rewriting. Thanks for your answer. – IMarco Aug 23 '17 at 13:14
  • After reviewing the api has been simple. Thank you for your answers. – IMarco Aug 28 '17 at 06:28

0 Answers0