[prev in list] [next in list] [prev in thread] [next in thread] List: fop-user Subject: Trouble going from Graphics2D to PDF, with PDFDocumentGraphics2D From: Jean-Yves Tinevez <tinevez () pasteur ! fr> Date: 2011-07-21 12:25:43 Message-ID: D7674F4E-2255-4799-90E5-57C37FA8AC64 () pasteur ! fr [Download RAW message or body] Dear list, I am struggling a lot with exporting a swing figure based on Graphics2D drawing to PDF using FOP. After a lot of fiddling and googling, I turn to you with hope. Basically, I have a panel that is painted with a draw(Graphics2D g2d, Rectangle bounds) methods (in my specific case it is a JFreeChart plot) that I try to export as pdf. I have tried to do that in two ways, leading to 2 problems. The first way I investigated was based on PDFDocumentGraphics2D I came with the following snippet in charge of the exporting: /** * Exports a JFreeChart to a PDF file. * <p> * We use a dirty hack for that: we first export to a physical SVG file, reload it, and * use Apache FOP PDF transcoder to convert it to pdfs. It only works partially, for * the text ends up in not being selectable in the pdf. * * @param chart JFreeChart to export * @param bounds the dimensions of the viewport * @param pdfFile the output file. * @throws IOException if writing the pdfFile fails. */ public static void exportChartAsPDF(JFreeChart chart, Rectangle bounds, File pdfFile) throws IOException, TranscoderException { PDFDocumentGraphics2D doc = new PDFDocumentGraphics2D(false); doc.setupDefaultFontInfo(); doc.setGraphicContext(new GraphicContext()); OutputStream stream = new FileOutputStream(pdfFile); doc.setupDocument(stream, bounds.width, bounds.height); doc.setDeviceDPI(60); chart.draw(doc, bounds ); stream.flush(); doc.finish(); stream.close(); } It turned out the execution of this method works fine, as long as I use standard fonts (Helvetica not Arial): I get a pdf file, which can be opened in the Mac OSX's Preview for instance, but not in Acrobat reader. In the later, I receive a message stating that the pdf has "some" error and that I should get contacted, and some parts of the pdf are not drawn. I attach an example of this pdf to this mail. Could you tip me on what I did wrong there? The versions of the libs I used: - FOP: SVN tags/fop-0_94 - Java: os.arch => x86_64 os.name => Mac OS X os.version => 10.5.8 java.version => 1.6.0_26 java.vendor => Apple Inc. java.runtime.name => Java(TM) SE Runtime Environment java.runtime.version => 1.6.0_26-b03-384-9M3425 java.vm.name => Java HotSpot(TM) 64-Bit Server VM java.vm.version => 20.1-b02-384 java.vm.vendor => Apple Inc. Cheers jy [Attachment #3 (multipart/mixed)] [Attachment #5 (text/html)] <html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \ -webkit-line-break: after-white-space; ">Dear list,<div><br></div><div>I am \ struggling a lot with exporting a swing figure based on Graphics2D drawing to PDF \ using FOP. After a lot of fiddling and googling, I turn to you with \ hope. </div><div><br></div><div>Basically, I have a panel that is painted with a \ draw(Graphics2D g2d, Rectangle bounds) methods (in my specific case it is a \ JFreeChart plot) that I try to export as pdf. I have tried to do that in two ways, \ leading to 2 problems. The first way I investigated was based on <span \ class="Apple-style-span" style="font-family: Monaco; font-size: 11px; \ "><u>PDFDocumentGraphics2D</u></span></div><div><br></div><div><span \ class="Apple-style-span" style="font-family: Monaco; font-size: 11px; \ "></span></div><div>I came with the following snippet in charge of the \ exporting:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ min-height: 15px; "><br></div><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ color: rgb(63, 95, 191); "><span style="color: #000000"><span class="Apple-tab-span" \ style="white-space:pre"> </span></span>/**</div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * Exports a JFreeChart to a PDF file.</div><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; color: rgb(63, 95, 191); "><span \ class="Apple-tab-span" style="white-space:pre"> </span> * <span style="color: \ #7f7f9f"><p></span></div><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * We use a dirty hack for that: we first export to a \ physical SVG file, reload it, and</div><div style="margin-top: 0px; margin-right: \ 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal \ Monaco; color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * use <span style="text-decoration: \ underline">Apache</span> FOP PDF <span style="text-decoration: \ underline">transcoder</span> to convert it to <span style="text-decoration: \ underline">pdfs</span>. It only works partially, for</div><div style="margin-top: \ 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal \ normal 11px/normal Monaco; color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * the text ends up in not being <span \ style="text-decoration: underline">selectable</span> in the <span \ style="text-decoration: underline">pdf</span>.</div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * </div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * <span style="color: #7f9fbf">@param</span> chart \ JFreeChart to export</div><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * <span style="color: #7f9fbf">@param</span> bounds \ the dimensions of the <span style="text-decoration: \ underline">viewport</span></div><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ color: rgb(63, 95, 191); "><span class="Apple-tab-span" \ style="white-space:pre"> </span> * <span style="color: #7f9fbf">@param</span> pdfFile \ the output file.</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: \ 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; color: rgb(63, \ 95, 191); "><span class="Apple-tab-span" style="white-space:pre"> </span> * <span \ style="color: #7f9fbf">@throws</span> IOException if writing the pdfFile \ fails.</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; \ margin-left: 0px; font: normal normal normal 11px/normal Monaco; color: rgb(63, 95, \ 191); "><span class="Apple-tab-span" style="white-space:pre"> </span> */</div><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span><span style="color: #7f0055">public</span> <span \ style="color: #7f0055">static</span> <span style="color: #7f0055">void</span> \ exportChartAsPDF(JFreeChart chart, Rectangle bounds, File pdfFile) <span \ style="color: #7f0055">throws</span> IOException, TranscoderException {</div><p \ style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: \ 15.0px"><span class="Apple-tab-span" style="white-space:pre"> </span><br \ class="webkit-block-placeholder"></p><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ "><span class="Apple-tab-span" style="white-space:pre"> </span>PDFDocumentGraphics2D \ doc = <span style="color: #7f0055">new</span> PDFDocumentGraphics2D(<span \ style="color: #7f0055">false</span>);</div><div style="margin-top: 0px; margin-right: \ 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal \ Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>doc.setupDefaultFontInfo();</div><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>doc.setGraphicContext(<span style="color: \ #7f0055">new</span> GraphicContext());</div><p style="margin: 0.0px 0.0px 0.0px \ 0.0px; font: 11.0px Monaco; min-height: 15.0px"><span class="Apple-tab-span" \ style="white-space:pre"> </span><br class="webkit-block-placeholder"></p><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>OutputStream stream = <span style="color: \ #7f0055">new</span> FileOutputStream(pdfFile);</div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>doc.setupDocument(stream, bounds.<span style="color: \ #0000c0">width</span>, bounds.<span style="color: #0000c0">height</span>);</div><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>doc.setDeviceDPI(60);</div><p style="margin: 0.0px \ 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px"><span \ class="Apple-tab-span" style="white-space:pre"> </span><br \ class="webkit-block-placeholder"></p><div style="margin-top: 0px; margin-right: 0px; \ margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; \ "><span class="Apple-tab-span" style="white-space:pre"> </span>chart.draw(doc, \ bounds );</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; \ margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><span \ class="Apple-tab-span" style="white-space:pre"> </span>stream.flush();</div><div \ style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; \ font: normal normal normal 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>doc.finish();</div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>stream.close();</div><div style="margin-top: 0px; \ margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal \ 11px/normal Monaco; "><span class="Apple-tab-span" \ style="white-space:pre"> </span>}</div></div><div><br></div><div><br></div><div>It \ turned out the execution of this method works fine, as long as I use standard fonts \ (Helvetica not Arial): I get a pdf file, which can be opened in the Mac OSX's Preview \ for instance, but not in Acrobat reader. </div><div>In the later, I receive a \ message stating that the pdf has "some" error and that I should get contacted, and \ some parts of the pdf are not drawn. I attach an example of this pdf to this \ mail.</div><div><br></div><div></div></body></html> ["ExportTest.pdf" (ExportTest.pdf)] %PDF-1.4 % 4 0 obj << /Producer (Apache FOP Version SVN tags/fop-0_94: PDFDocumentGraphics2D) /CreationDate (D:20110721140028+02'00') > > endobj 6 0 obj << /Length 8 0 R /Filter /FlateDecode > > stream x]ˎ,qWRZƃ5``B˘l`ɬSRT0UŬ$A9:F?y4= \ 8Co?|y}[?0l"sZ|}_~uǷo~O/rw|[N \ ם~"wjf91[Ƿǯ~ן7o?z͟?/_[h.5( \ j&GbswCz_:zXut\z "Lx^x ^Il9O?/>