| 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. |
ExportTest.pdf
Description: Adobe PDF document
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 |
