IText,蜡染和vaadin图表在linux上出错(IText, batik and vaadin charts scales wrong on linux)

编程入门 行业动态 更新时间:2024-10-21 23:15:21
IText,蜡染和vaadin图表在linux上出错(IText, batik and vaadin charts scales wrong on linux)

我在将图表输出为PDF时遇到了一些麻烦。 最近我发布了这个: 使用iText和蜡染生成PDF,这是根据建议解决的一些调整到尺度。

我在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的某个地方。 谁知道这件事?

I've been having some trouble getting my charts out to PDF. Recently i posted this: Generating PDF with iText and batik which was solved as suggested with some tweaking to the scales.

I run amy testenviroment on a local glassfishserver on a windows 10 machine, and when I export to PDF I actually get a pretty result now.

But when I pushed the results to the RHEL server, the results differed. The charts shown on the website is great, but when I export to pdf, I get this:

As you can see, the title is pushed down, for some reason the Y-axis with labels are cropped, and the data-labels are squished together. I've tried playing around with different scales, with and without scaletofit, scaletoabsolute and so on, but no matter what I do, it keeps doing that weird thing.

Does anybody has any idea whats going on - and even better, how to fix it? I've doublechecked that phantomjs is the same version, to make sure the SVG is the right one-.

The code is as follows:

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; }

UPDATE I've tried reading a SVG file from disk, that I knew was correct, and that is put correctly within the PDF. So the problem lies somewhere within the SVG Generator. Anyone knows about this?

最满意答案

使用旧版本的PhantomJS(1.9.8)可以解决问题。 我和瓦丁一起买了票。

Using an older version of PhantomJS (1.9.8) fixes the problem. I've made a ticket with Vaadin.

更多推荐

本文发布于:2023-08-04 23:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蜡染   图表   vaadin   IText   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!