4

我正在尝试从 HTML 生成 Java 中的 pdf 文件。HTML 代码包含由 Google Charts API 生成的 svg 标记,用于显示柱形图。

我试着用 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();

结果是生成的 PDF,其中包含图表的轴值,但不包含图表本身;即不包括图表图像。

任何想法或建议都会非常有帮助。

谢谢,肖恩

4

1 回答 1

3

希望您找到了解决问题的方法 - 已经一个月了。

但是,如果您仍在寻找,并且如果您对要呈现为 PDF 的页面有足够的控制权,则可以尝试使用蜡染 ( http://xmlgraphics.apache.org/batik/ )。我继承了一个较旧的 grails 应用程序,该应用程序从作为网页查看时确实包含 SVG 的报告创建 PDF。该代码首先从 SVG 元素创建一个 PNG,然后使用 iText 渲染器创建 PDF。

在 batik 项目页面中,有对 PDF 生成的引用,但我找不到将包含一些 SVG 和一些“正常”标记的 DOM 并呈现为 PDF 的示例。

于 2012-06-05T20:16:39.663 回答