2016-06-10 26 views
0

我一直有一些麻煩讓我的圖表出PDF。 最近我發佈了這個:Generating PDF with iText and batik這是解決建議與一些調整的規模。IText,蠟染和vaadin圖表在Linux上縮小比例

我在Windows 10計算機上的本地Glassfish服務器上運行amy testenviroment,當我導出爲PDF時,我實際上得到了一個漂亮的結果。

Windows result

但是,當我推,結果到RHEL服務器,結果不同。在網站上顯示的圖表是偉大的,但是當我導出爲PDF格式,我得到這個:

Linux result

正如你所看到的,標題被按下時,由於某種原因,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; 
} 

UPDATE我試圖從磁盤讀取一個SVG文件,我知道是正確的,那就是在PDF中放正確。所以問題在於SVG Generator的某處。任何人都知道這件事?

+0

沒人?我似乎無法在任何地方找到任何東西。 : -/ –

+0

編輯之後,你排除了pdf,你可以發佈一個簡化版本的代碼,只包含svg的東西? –

+0

@AmedeeVanGasse - 現在應該做,好點。儘管我仍然相信錯誤在於使用SVGGenerator單例的行。我嘗試更新PhantomJS所需的所有軟件包,嚴格遵循其網站的指示,但我仍然沒有運氣。 :) –

回答

1

使用舊版本的PhantomJS(1.9.8)可以解決問題。 我已經和Vaadin訂了一張票。

+0

好吧,很好,你找到了原因。我刪除了itext標記,以便它不會在我們的Slack中彈出(是的,我們在iText Software監視器StackOverflow中)。 –

+0

很棒 - 很棒的服務。 –