ColumnText和带有Image Base64标记的HTML

时间:2016-08-30 16:30:31

标签: base64 itext html-to-pdf

我正在使用this example来显示HTML标头和HTML页脚。

但是当html有一个图像元素(base64)不起作用时。

我试过了another example。但这并没有使用ColumnText。

我尝试添加

htmlContext.setImageProvider(new Base64ImageProvider());

TagProcessorFactory htmlTagProcessorFactory = Tags.getHtmlTagProcessorFactory();
htmlTagProcessorFactory.removeProcessor(HTML.Tag.IMG);
htmlTagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);

但没有一次尝试成功。

2 个答案:

答案 0 :(得分:1)

我设法让它发挥作用。但是用表。以这种方式,base64图像出现在PDF文档

    //CSS
    CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);

    // HTML
    TagProcessorFactory htmlTagProcessorFactory = Tags.getHtmlTagProcessorFactory();
    //htmlTagProcessorFactory.removeProcessor(HTML.Tag.IMG);
    //htmlTagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(htmlTagProcessorFactory);
    htmlContext.setImageProvider(new Base64ImageProvider());

    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, (Pipeline) null);

    HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, htmlPipeline);

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

    PdfPTable table = new PdfPTable(1);

    PdfPCell cell = new PdfPCell();
    cell.setPadding(0);
    cell.setUseBorderPadding(false);
    cell.setBorder(0);

    for (Element e : elements) {
        cell.addElement(e);
    }

    table.addCell(cell);

    // Adding a table at an absolute position
    table.writeSelectedRows(0, -1, x, y, writer.getDirectContent());

答案 1 :(得分:0)

使用您所做的相同示例,我让它在不需要表的情况下工作,只有ColumnText。您只需将每个元素添加到ColumnText对象,如下所示。

    // step 3
    document.open();
    // step 4
    PdfContentByte canvas = writer.getDirectContent();
    ColumnText ct = new ColumnText(canvas);
    int side_of_the_page = 0;
    ct.setSimpleColumn(COLUMNS[side_of_the_page]);
    ct.setIgnoreSpacingBefore(false);

   // CSS
   CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);

   // ELEMENT
   ElementList elements = new ElementList();
   ElementHandlerPipeline end = new ElementHandlerPipeline(elements, (Pipeline) null);

   // HTML
   HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
   htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
   htmlContext.setImageProvider(new Base64ImageProvider());

   // Pipelines
   HtmlPipeline html = new HtmlPipeline(htmlContext, end);
   CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

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

   for (Element element : elements) {
         ct.addElement(element);
   }

   // step 5
   document.close();