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:verlag.preis...@t-online.de] Sent: 07 February 2012 12:20 To: fop-users@xmlgraphics.apache.org 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: fop-users-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: fop-users-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org