简体   繁体   English

Java / iText / Flying Saucer:将包含svg标签的HTML转换为pdf

[英]Java/iText/Flying Saucer: Convert HTML containing svg tags to pdf

I'm trying to generate a pdf file in Java from HTML. 我正在尝试用HTML生成Java格式的pdf文件。 The HTML code contains a svg tag that was generated by Google Charts API to display a column chart. HTML代码包含由Google Charts API生成的svg标记,用于显示柱形图。

I tried to do it with Flying Saucer R8 like so: 我尝试用Flying Saucer R8这样做:

  StringBuffer sb = new StringBuffer();
    sb.append("<div id='chartArea'><svg width='830' height='400'><defs id='defs'><clipPath id='_ABSTRACT_RENDERER_ID_0'><rect x='45' y='77' width='560' height='247'/></clipPath></defs><rect x='0' y='0' width='830' height='400' stroke='none' stroke-width='0' fill='#ffffff'/><g><rect x='45' y='77' width='560' height='247' stroke='none' stroke-width='0' fill-opacity='0' fill='#ffffff'/><g clip-path='url(#_ABSTRACT_RENDERER_ID_0)'><g><rect x='45' y='323' width='560' height='1' stroke='none' stroke-width='0' fill='#cccccc'/><rect x='45' y='262' width='560' height='1' stroke='none' stroke-width='0' fill='#cccccc'/><rect x='45' y='200' width='560' height='1' stroke='none' stroke-width='0' fill='#cccccc'/><rect x='45' y='139' width='560' height='1' stroke='none' stroke-width='0' fill='#cccccc'/><rect x='45' y='77' width='560' height='1' stroke='none' stroke-width='0' fill='#cccccc'/></g><g><rect x='60' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='140' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='220' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='300' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='380' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='460' y='323.5' width='49' height='0' stroke='none' stroke-width='0' fill='#757575'/><rect x='540' y='111' width='49' height='212' stroke='none' stroke-width='0' fill='#757575'/></g><g><rect x='45' y='323' width='560' height='1' stroke='none' stroke-width='0' fill='#333333'/></g></g><g/><g><g><text text-anchor='middle' x='85.42857142857143' y='343.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-04-26</text></g><g><text text-anchor='middle' x='165.28571428571428' y='360.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-04-27</text></g><g><text text-anchor='middle' x='245.14285714285717' y='343.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-04-28</text></g><g><text text-anchor='middle' x='325' y='360.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-04-29</text></g><g><text text-anchor='middle' x='404.8571428571429' y='343.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-04-30</text></g><g><text text-anchor='middle' x='484.7142857142857' y='360.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-05-01</text></g><g><text text-anchor='middle' x='564.5714285714286' y='343.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#222222'>2012-05-02</text></g><g><text text-anchor='end' x='39' y='328.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#444444'>0</text></g><g><text text-anchor='end' x='39' y='266.55' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#444444'>6</text></g><g><text text-anchor='end' x='39' y='205.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#444444'>12</text></g><g><text text-anchor='end' x='39' y='143.55' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#444444'>18</text></g><g><text text-anchor='end' x='39' y='82.05' font-family='Arial' font-size='13' stroke='none' stroke-width='0' fill='#444444'>24</text></g></g></g><g><g><text text-anchor='middle' x='17.05' y='200.5' font-family='Arial' font-size='13' font-style='italic' transform='rotate(-90 17.05 200.5)' stroke='none' stroke-width='0' fill='#222222'>Amount (USD)</text></g></g><g/></svg></div>");

    Document document = XMLResource.load(new ByteArrayInputStream(sb.toString().getBytes())).getDocument();
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(document,"test");
    String outputFile = "test.pdf";
    OutputStream os = new FileOutputStream(outputFile);
    renderer.layout();
    renderer.createPDF(os);
    os.close();

The result is a generated PDF that contains the chart's axis values but not the chart itself; 结果是生成的PDF包含图表的轴值,但不包含图表本身; ie the chart image was not included. 即图表图像未包括在内。

Any ideas or suggestions will be extremely helpful. 任何想法或建议都将非常有帮助。

Thanks, Shaun 谢谢,肖恩

Hopefully you've found a solution to your problem - it's been a month. 希望你找到解决问题的方法 - 已经有一个月了。

However, if you are still looking, and if you have control enough over the page you want to render to PDF, you could try using batik ( http://xmlgraphics.apache.org/batik/ ). 但是,如果您仍在寻找,并且如果您对要渲染为PDF的页面有足够的控制权,则可以尝试使用batik( http://xmlgraphics.apache.org/batik/ )。 I have inherited an older grails app that creates a PDF from a report that does have SVG in it when viewed as a web page. 我继承了一个旧的grails应用程序,该应用程序从一个报表中创建一个PDF,当它被视为一个网页时,它会包含SVG。 The code first creates a PNG from the SVG elements, then uses the iText renderer to create the PDF. 代码首先从SVG元素创建PNG,然后使用iText渲染器创建PDF。

From the batik project page, there are references to PDF generation, but I couldn't find an example that would take a DOM including some SVG and some "normal" markup and render to PDF. 从蜡染项目页面,有PDF生成的参考,但我找不到一个将DOM包括一些SVG和一些“正常”标记并渲染为PDF的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM