[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.&nbsp;</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&nbsp;<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">&lt;p&gt;</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> *&nbsp;</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.&nbsp;</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?/> \
i;/>Aѷχ]8&&"o~kSoO(<g|fҧ!nOIyLȵ.,Ddכ7R织|>??qC_|S-7 \
Y0'^+_C y<<ɴyۑ'($Z2`	 (nH \
Af>?'xrRh0"ۡ'#j{ImMj#R4,n~7lvh1g''g3z'ۡg#uVONf \
$B?COF1՜|nR޵Wg#u֭bq>9hwRz2R&S>_ܤ6jr7_=ڨNH0ONkc@enhVM@>Tކ \
ÿs|n'7,q~$%!sFi.z \
0#mHg%|lN7!<,a3DŽ< \
8LJZV	xY	X^3nCx=D,̛мO&0DT/kig\(P:4꟱+OJuX0CK~'8SSʓR=0RX0T?hx/M<)
  #^ s+H~?^y*kl@Gzk4ZB+_ \
Dߤ5	eR!KZƌW<<GY?ښŋ鍷ʱXȷ5ܳ< \
gVϿtzۯ_c OW,mY͏J{FlǁS
ܛuG8}W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Helvetica
/Encoding /WinAnsiEncoding >>
endobj
8 0 obj
5315
endobj
1 0 obj
<< /Type /Pages
/Count 1
/Kids [5 0 R ] >>
endobj
2 0 obj
<< /Type /Catalog
 /Pages 1 0 R
> > 
endobj
3 0 obj
<<
/Font <<
  /F1 7 0 R
> > 
/ProcSet [ /PDF /ImageB /ImageC /Text ]
> > 
endobj
xref
0 9
0000000000 65535 f 
0000005822 00000 n 
0000005880 00000 n 
0000005930 00000 n 
0000000015 00000 n 
0000005538 00000 n 
0000000148 00000 n 
0000005695 00000 n 
0000005802 00000 n 
trailer
<<
/Size 9
/Root 2 0 R
/Info 4 0 R
/ID [<9B43174F02C9ABEB899E74742CD8DCF1> <9B43174F02C9ABEB899E74742CD8DCF1>]
> > 
startxref
6015
%%EOF


[Attachment #7 (text/html)]

<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
-webkit-line-break: after-white-space; "><div></div><div><br></div><div>Could you tip \
me on what I did wrong there?</div><div><br></div><div>The versions of the libs I \
used:</div><div><br></div><div>- FOP:&nbsp;<span class="Apple-style-span" \
style="font-family: Monaco; font-size: 11px; ">SVN \
tags/fop-0_94</span></div><div><span class="Apple-style-span" style="font-family: \
Monaco; font-size: 11px; ">- Java:</span></div><div><span class="Apple-style-span" \
style="font-family: Monaco; font-size: 11px; "><div>&nbsp;&nbsp;os.arch =&gt; \
x86_64</div><div>&nbsp;&nbsp;os.name =&gt; Mac OS X</div><div>&nbsp;&nbsp;os.version \
=&gt; 10.5.8</div><div>&nbsp;&nbsp;java.version =&gt; \
1.6.0_26</div><div>&nbsp;&nbsp;java.vendor =&gt; Apple \
Inc.</div><div>&nbsp;&nbsp;java.runtime.name =&gt; Java(TM) SE Runtime \
Environment</div><div>&nbsp;&nbsp;java.runtime.version =&gt; \
1.6.0_26-b03-384-9M3425</div><div>&nbsp;&nbsp;java.vm.name =&gt; Java HotSpot(TM) \
64-Bit Server VM</div><div>&nbsp;&nbsp;java.vm.version =&gt; \
20.1-b02-384</div><div>&nbsp;&nbsp;java.vm.vendor =&gt; Apple \
Inc.</div></span></div><div><br></div><div><br></div><div>Cheers</div><div>jy</div><div><br></div></body></html>




[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic