Generating a PDF by drawing to PDFDocumentGraphics2D

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating a PDF by drawing to PDFDocumentGraphics2D

Konstantin Preißer-2
Hi all,

I'm sorry if this is the wrong place for my question.

I was seaching for a way to dynamically generate a PDF in Java code by drawing to a Graphics2D object. When I googled this, I found Apache FOP 1.0 which has a org.apache.fop.svg.PDFDocumentGraphics2D class (derived from Graphics2D), which seems to support dynamically generating a PDF by drawing to it.

E.g., I use code like this to generate a PDF and write it to an OutputStream (using Java 1.7):

    PDFDocumentGraphics2D g = new PDFDocumentGraphics2D(false, out, 842, 595);

    g.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

    g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
    g.drawLine(10, 10, 100, 100);
    g.drawString("Hi!", 30, 30);

    g.nextPage(); // switch to next page

    g.drawString("This is the 2nd page.", 30, 30);


    g.finish();
    g.dispose();

This seems to be working really well, using the integrated PDF Fonts.

However, I now have the problem that these integrated fonts seem not to support Unicode characters, e.g. if I use

g.drawString("\u263a", 30, 30);

then the Character is substituted by a "#" and a warning is printed:

Feb 07, 2012 1:10:11 PM org.apache.fop.fonts.Typeface warnMissingGlyph
Warnung: Glyph 9786 (0x263a, smileface) not available in font Helvetica

So, I was searching for a way to embed a TTF font in the PDF. However, I wasn't successful finding information about it, as it seems the FOP documentation and examples are only about XSL-FO (which I don't know much about, as I only need to generate PDFs by drawing to a Graphics2D), but using a PDFDocumentGraphics2D seems not to be well-documented. So I'm not sure if this is even an intended way to use FOP, or if there are other/better solutions to generate a PDF by drawing to a Graphics2D object in Java.

My questions are:
Is using FOP for generating a PDF by drawing to a Graphics2D a recommended way?
Is it possible to embed TTF fonts when drawing to a PDFDocumentGraphics2D object? If yes, how?


Thanks!

Best Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Generating a PDF by drawing to PDFDocumentGraphics2D

Theresa Jayne Forster
The usual way we create PDFs is via XML/XSL into a .fo file which is then "compiled" into the pdf.

Example of the code
public File generatePDFFromXml(File xslFile,File xmlFile) {
        BufferedOutputStream out = null;
        File tempFile = null;
            fopFactory.setStrictValidation(false);
            DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
            Configuration cfg = cfgBuilder.buildFromFile(new File("fopconfig.xml"));
            fopFactory.setUserConfig(cfg);

            tempFile = File.createTempFile("W2P", ".pdf");
            out = new BufferedOutputStream(new FileOutputStream(tempFile));
            FOUserAgent useragent= fopFactory.newFOUserAgent();
            useragent.setOutputFile(tempFile);
            useragent.setTargetResolution(300);
            System.out.println("Creating New Fop");
            long timer = System.currentTimeMillis();
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,useragent,out);
            System.out.println("Creation of Fop took "+(System.currentTimeMillis()-timer)+" Miliseconds");
            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(xslFile);
            Transformer transformer = factory.newTransformer(xslt);
            Source src = new StreamSource(xmlFile);
            Result res = new SAXResult(fop.getDefaultHandler());
            System.out.println("Creating PDF");
            timer = System.currentTimeMillis();            
            transformer.transform(src, res);
            System.out.println("Creation of PDF took "+(System.currentTimeMillis()-timer)+" Miliseconds");
           out.flush();
           out.close();
}

The XSL and xml can do most anything but a snippet is below of the xsl

<fo:page-sequence master-reference="propdetails">
        <fo:static-content flow-name="xsl-region-before">
                <fo:block-container position="absolute" top="0mm" left="0mm" width="210mm" height="49mm" >
                        <xsl:call-template name="property_header">
                                <xsl:with-param name="width">214</xsl:with-param>
                                <xsl:with-param name="height">53</xsl:with-param>
                        </xsl:call-template>
                </fo:block-container>
                <fo:block-container position="absolute" top="56mm" left="11mm" width="188mm" height="113mm" overflow="hidden">
                        <fo:block>
                                <xsl:variable name="picurl">
                                        <xsl:value-of select="/document/data/body/descendant-or-self::*[@id='large']/@src"/>
                                </xsl:variable>
                                <fo:external-graphic width="188mm" height="113mm" content-width="188mm" content-height="auto" scaling-method="resample-any-method" src="{$picurl}" />
                        </fo:block>
                </fo:block-container>
                <fo:block-container position="absolute" top="173mm" left="10mm" width="57mm" height="22mm">
                        <fo:block>
                                <fo:instream-foreign-object>
                                        <svg:svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                                         version="1.1" width="57mm" height="22mm" id="roundedbox" viewbox="0 0 57 22"> <g>
                                         <rect x="0mm" y="0mm" width="57mm" height="22mm" rx="5" ry="5" style="stroke: white; fill: #005924;"/>
                                        </g>
                                </svg:svg>
                                </fo:instream-foreign-object>
                        </fo:block>

Hope this helps.

Kindest regards


Theresa Forster
Senior Software Developer


-----Original Message-----
From: Konstantin Preißer [mailto:[hidden email]]
Sent: 07 February 2012 12:20
To: [hidden email]
Subject: Generating a PDF by drawing to PDFDocumentGraphics2D

Hi all,

I'm sorry if this is the wrong place for my question.

I was seaching for a way to dynamically generate a PDF in Java code by drawing to a Graphics2D object. When I googled this, I found Apache FOP 1.0 which has a org.apache.fop.svg.PDFDocumentGraphics2D class (derived from Graphics2D), which seems to support dynamically generating a PDF by drawing to it.

E.g., I use code like this to generate a PDF and write it to an OutputStream (using Java 1.7):

    PDFDocumentGraphics2D g = new PDFDocumentGraphics2D(false, out, 842, 595);

    g.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

    g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
    g.drawLine(10, 10, 100, 100);
    g.drawString("Hi!", 30, 30);

    g.nextPage(); // switch to next page

    g.drawString("This is the 2nd page.", 30, 30);


    g.finish();
    g.dispose();

This seems to be working really well, using the integrated PDF Fonts.

However, I now have the problem that these integrated fonts seem not to support Unicode characters, e.g. if I use

g.drawString("\u263a", 30, 30);

then the Character is substituted by a "#" and a warning is printed:

Feb 07, 2012 1:10:11 PM org.apache.fop.fonts.Typeface warnMissingGlyph
Warnung: Glyph 9786 (0x263a, smileface) not available in font Helvetica

So, I was searching for a way to embed a TTF font in the PDF. However, I wasn't successful finding information about it, as it seems the FOP documentation and examples are only about XSL-FO (which I don't know much about, as I only need to generate PDFs by drawing to a Graphics2D), but using a PDFDocumentGraphics2D seems not to be well-documented. So I'm not sure if this is even an intended way to use FOP, or if there are other/better solutions to generate a PDF by drawing to a Graphics2D object in Java.

My questions are:
Is using FOP for generating a PDF by drawing to a Graphics2D a recommended way?
Is it possible to embed TTF fonts when drawing to a PDFDocumentGraphics2D object? If yes, how?


Thanks!

Best Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Generating a PDF by drawing to PDFDocumentGraphics2D

Konstantin Preißer-2
Hi Theresa,


> -----Original Message-----
> From: Theresa Jayne Forster [mailto:[hidden email]]
> Sent: Tuesday, February 07, 2012 1:43 PM
> To: [hidden email]
> Subject: RE: Generating a PDF by drawing to PDFDocumentGraphics2D
>
> The usual way we create PDFs is via XML/XSL into a .fo file which is
> then "compiled" into the pdf.
>

Thank you for your answer.

Unfortunately, I don't know much about creating a PDF via XML/XSL into a .fo file, and I don't intend to use it, as I only need to generate a PDF dynamically by drawing to a Graphics2D object - which is why I'm using FOP's PDFDocumentGraphics2D.

I know about Apache PDFBox where you can generate a PDF on the fly, but it seems that its API doesn't have support for drawing to a Graphics2D object.

Another possiblility would be using the iText (R) library; however, it seems to have some problems with text positioning, and it's licence applies several restrictions on what to do with the product.

Therefore, I'm currently using Apache FOP 1.0 to generating a PDF by drawing to a PDFDocumentGraphics2D, which seems to work pretty well.
The only problem I'm currently having is how to embed Fonts into the PDF, so that I also can use other characters which are not available in the PDF's included fonts (or e.g. change the Font charset, so that other Unicode characters are available).

Do you have an idea if this is possible?

Thanks!


Best Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Generating a PDF by drawing to PDFDocumentGraphics2D

Angelo zerr
In reply to this post by Theresa Jayne Forster
Hi Konstantin,

Your feature about creating PDF Document with Java code PDFDocumentGraphics2D (and without XML-FO), is exactly what we need in XDocReport  for our converter
Docx->PDF and ODT->PDF wich works with iText. We wish to give our code to Apache but problem is iText. So we need create PDF Document
with Java code PDFDocumentGraphics2D.

I have tried to investiage to manage that (have a generic structured document which generate PDF Document with PDFDocumentGraphics2D ) but it's very very hard to code
because of layout management. I'm waiting for developer which are interested to create a generic document like iText and I will  be happy if we can share our skill (or perhaps working in the same project?)

Regards Angelo



Le 20 février 2012 18:25, Konstantin Preißer <[hidden email]> a écrit :
Hi Theresa,


> -----Original Message-----
> From: Theresa Jayne Forster [mailto:[hidden email]]
> Sent: Tuesday, February 07, 2012 1:43 PM
> To: [hidden email]
> Subject: RE: Generating a PDF by drawing to PDFDocumentGraphics2D
>
> The usual way we create PDFs is via XML/XSL into a .fo file which is
> then "compiled" into the pdf.
>

Thank you for your answer.

Unfortunately, I don't know much about creating a PDF via XML/XSL into a .fo file, and I don't intend to use it, as I only need to generate a PDF dynamically by drawing to a Graphics2D object - which is why I'm using FOP's PDFDocumentGraphics2D.

I know about Apache PDFBox where you can generate a PDF on the fly, but it seems that its API doesn't have support for drawing to a Graphics2D object.

Another possiblility would be using the iText (R) library; however, it seems to have some problems with text positioning, and it's licence applies several restrictions on what to do with the product.

Therefore, I'm currently using Apache FOP 1.0 to generating a PDF by drawing to a PDFDocumentGraphics2D, which seems to work pretty well.
The only problem I'm currently having is how to embed Fonts into the PDF, so that I also can use other characters which are not available in the PDF's included fonts (or e.g. change the Font charset, so that other Unicode characters are available).

Do you have an idea if this is possible?

Thanks!


Best Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Generating a PDF by drawing to PDFDocumentGraphics2D

Luis Bernardo
In reply to this post by Theresa Jayne Forster

Yes, it is possible but it is certainly an uncommon way to use the FOP code!

Attached is an example, together with a font configuration file, and the
output. The example is a slightly edited version of the
ExampleJava2D2PDF.java of the emdedding package (in
example/embedding/java). The example was run on a Mac so edit the fonts
to your environment.

On 2/20/12 5:25 PM, Konstantin Preißer wrote:

> Hi Theresa,
>
>
>> -----Original Message-----
>> From: Theresa Jayne Forster [mailto:[hidden email]]
>> Sent: Tuesday, February 07, 2012 1:43 PM
>> To: [hidden email]
>> Subject: RE: Generating a PDF by drawing to PDFDocumentGraphics2D
>>
>> The usual way we create PDFs is via XML/XSL into a .fo file which is
>> then "compiled" into the pdf.
>>
> Thank you for your answer.
>
> Unfortunately, I don't know much about creating a PDF via XML/XSL into a .fo file, and I don't intend to use it, as I only need to generate a PDF dynamically by drawing to a Graphics2D object - which is why I'm using FOP's PDFDocumentGraphics2D.
>
> I know about Apache PDFBox where you can generate a PDF on the fly, but it seems that its API doesn't have support for drawing to a Graphics2D object.
>
> Another possiblility would be using the iText (R) library; however, it seems to have some problems with text positioning, and it's licence applies several restrictions on what to do with the product.
>
> Therefore, I'm currently using Apache FOP 1.0 to generating a PDF by drawing to a PDFDocumentGraphics2D, which seems to work pretty well.
> The only problem I'm currently having is how to embed Fonts into the PDF, so that I also can use other characters which are not available in the PDF's included fonts (or e.g. change the Font charset, so that other Unicode characters are available).
>
> Do you have an idea if this is possible?
>
> Thanks!
>
>
> Best Regards,
> Konstantin Preißer
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

ExampleJava2D2PDF.java (9K) Download Attachment
fop-java2d2pdffonts.xconf (1K) Download Attachment
ResultJava2D2PDF.pdf (41K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Generating a PDF by drawing to PDFDocumentGraphics2D

Konstantin Preißer-2

> -----Original Message-----
> From: Luis Bernardo [mailto:[hidden email]]
> Sent: Tuesday, February 28, 2012 1:36 AM
> To: [hidden email]
> Subject: Re: Generating a PDF by drawing to PDFDocumentGraphics2D
>
>
> Yes, it is possible but it is certainly an uncommon way to use the FOP
> code!
>
> Attached is an example, together with a font configuration file, and
> the
> output. The example is a slightly edited version of the
> ExampleJava2D2PDF.java of the emdedding package (in
> example/embedding/java). The example was run on a Mac so edit the fonts
> to your environment.


Hello Luis,

thank you very much! I used you example for embedding Fonts and it is working fine. :)


Best Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]