我在将图表输出为PDF时遇到了一些麻烦。 最近我发布了这个:Generating PDF with iText and batik这是根据建议解决的,并对比例进行了一些调整。
我在Windows 10机器上的本地glassfish服务器上运行amy testenviroment,当我导出为PDF时,我实际上得到了一个漂亮的结果。
但是当我将结果推送到RHEL服务器时,结果会有所不同。网站上显示的图表很棒,但是当我导出为pdf时,我得到了这个:
正如您所看到的,标题被按下,由于某种原因,带有标签的Y轴被裁剪,数据标签被挤压在一起。我尝试过不同尺度的游戏,有没有scaletofit,scaletoabsolute等等,但无论我做什么,它都会做那种奇怪的事情。
有没有人知道最新情况 - 甚至更好,如何解决?我已经仔细检查过phantomjs是同一个版本,以确保SVG是正确的 - 。
代码如下:
private Image createSvgImage(PdfContentByte contentByte, Chart chart) throws IOException {
Configuration configuration = chart.getConfiguration();
configuration.setExporting(false);
SVGGenerator generator = SVGGenerator.getInstance();
generator.withHeigth(600);
generator.withWidth(1200);
String svg = generator.generate(configuration);
Image image = drawUnscaledSvg(contentByte, svg);
image.scaleToFit(800, 370);
configuration.setExporting(true);
return image;
}
private Image drawUnscaledSvg(PdfContentByte contentByte, String svgStr) throws IOException {
GraphicsNode imageGraphics = buildBatikGraphicsNode(svgStr);
float width = 1200;
float height = 600;
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics = template.createGraphics(width, height);
try {
imageGraphics.paint(graphics);
graphics.translate(-10, -10);
return new ImgTemplate(template);
} catch (BadElementException e) {
throw new RuntimeException("Couldn't generate PDF from SVG", e);
} finally {
graphics.dispose();
}
}
private GraphicsNode buildBatikGraphicsNode(String svgStr) throws IOException {
UserAgent agent = new UserAgentAdapter();
SVGDocument svgdoc = createSVGDocument(svgStr, agent);
DocumentLoader loader = new DocumentLoader(agent);
BridgeContext bridgeContext = new BridgeContext(agent, loader);
bridgeContext.setDynamicState(BridgeContext.STATIC);
GVTBuilder builder = new GVTBuilder();
GraphicsNode imageGraphics = builder.build(bridgeContext, svgdoc);
return imageGraphics;
}
private SVGDocument createSVGDocument(String svg, UserAgent agent)
throws IOException {
SVGDocumentFactory documentFactory = new SAXSVGDocumentFactory(
agent.getXMLParserClassName(), true);
SVGDocument svgdoc = documentFactory.createSVGDocument(null,
new StringReader(svg));
return svgdoc;
}
更新我试过从磁盘读取SVG文件,我知道这是正确的,并且在PDF中正确放置。所以问题出在SVG Generator的某个地方。有人知道吗?
答案 0 :(得分:1)
使用旧版本的PhantomJS(1.9.8)可以解决问题。 我和瓦丁一起买了票。