java
  • pdf
  • itext
  • flying-saucer
  • 2012-05-03 2 views 4 likes 
    4

    HTML에서 Java로 PDF 파일을 생성하려고합니다. HTML 코드에는 Google Charts API가 세로 막 대형 차트를 표시하기 위해 생성 한 svg 태그가 포함되어 있습니다. Java/iText/Flying Saucer : svg 태그가 포함 된 HTML을 PDF로 변환

    나는과 같이 비행 접시 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가 아닌 차트 자체; 즉 차트 이미지는 포함되지 않았습니다.

    모든 아이디어 나 제안 사항은 매우 유용합니다.

    감사합니다, 숀

    답변

    3

    바라건대 당신은 당신의 문제에 대한 해결책을 발견했습니다 - 그건되어 한 달.

    그러나 여전히 찾고있는 페이지에서 PDF로 렌더링하려는 페이지를 충분히 제어 할 수 있다면 바틱 (http://xmlgraphics.apache.org/batik/)을 사용해 볼 수 있습니다. 나는 웹 페이지로 볼 때 SVG를 가지고있는 보고서로부터 PDF를 생성하는 오래된 grails app을 상속 받았다. 이 코드는 먼저 SVG 요소에서 PNG를 만든 다음 iText 렌더러를 사용하여 PDF를 만듭니다.

    batik 프로젝트 페이지에서 PDF 생성에 대한 참조가 있지만 일부 SVG와 일부 "일반"마크 업을 포함하고 DOM에 PDF를 렌더링하는 예제는 찾을 수 없습니다.

     관련 문제

    • 관련 문제 없음^_^