1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id: ExampleJava2D2PDF.java 1851806 2019-01-22 11:53:31Z ssteiner $ */ 19 20 package embedding; 21 22 import java.awt.Color; 23 import java.awt.Dimension; 24 import java.awt.Font; 25 import java.awt.Graphics2D; 26 import java.io.File; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 import java.io.StringReader; 30 31 import javax.swing.JEditorPane; 32 33 import org.apache.commons.io.IOUtils; 34 35 import org.apache.xmlgraphics.util.UnitConv; 36 37 import org.apache.fop.configuration.Configuration; 38 import org.apache.fop.configuration.ConfigurationException; 39 import org.apache.fop.configuration.DefaultConfiguration; 40 import org.apache.fop.svg.PDFDocumentGraphics2D; 41 import org.apache.fop.svg.PDFDocumentGraphics2DConfigurator; 42 43 /** 44 * This example class demonstrates the use of {@link PDFDocumentGraphics2D} that can be 45 * used to create a PDF file from Java2D graphics (using the {@link Graphics2D} API). 46 */ 47 public class ExampleJava2D2PDF { 48 createAutoFontsConfiguration()49 private Configuration createAutoFontsConfiguration() { 50 //Create a default configuration using auto-detection of fonts. 51 //This can be a bit slow but covers most use cases. 52 DefaultConfiguration c = new DefaultConfiguration("cfg"); 53 DefaultConfiguration fonts = new DefaultConfiguration("fonts"); 54 c.addChild(fonts); 55 DefaultConfiguration autodetect = new DefaultConfiguration("auto-detect"); 56 fonts.addChild(autodetect); 57 return c; 58 59 /* You can also load the configuration from a file: 60 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 61 return cfgBuilder.buildFromFile(configFile); 62 */ 63 } 64 configure(PDFDocumentGraphics2D g2d, Configuration cfg)65 private void configure(PDFDocumentGraphics2D g2d, Configuration cfg) 66 throws ConfigurationException { 67 68 PDFDocumentGraphics2DConfigurator configurator = new PDFDocumentGraphics2DConfigurator(); 69 boolean useComplexScriptFeatures = false; 70 configurator.configure(g2d, cfg, useComplexScriptFeatures); 71 } 72 73 /** 74 * Creates a PDF file. The contents are painted using a Graphics2D implementation that 75 * generates an PDF file. 76 * @param outputFile the target file 77 * @throws IOException In case of an I/O error 78 * @throws ConfigurationException if an error occurs configuring the PDF output 79 */ generatePDF(File outputFile)80 public void generatePDF(File outputFile) throws IOException, ConfigurationException { 81 OutputStream out = new java.io.FileOutputStream(outputFile); 82 out = new java.io.BufferedOutputStream(out); 83 try { 84 85 //Instantiate the PDFDocumentGraphics2D instance 86 PDFDocumentGraphics2D g2d = new PDFDocumentGraphics2D(false); 87 g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext()); 88 89 //Configure the G2D with the necessary fonts 90 configure(g2d, createAutoFontsConfiguration()); 91 92 //Set up the document size 93 Dimension pageSize = new Dimension( 94 (int)Math.ceil(UnitConv.mm2pt(210)), 95 (int)Math.ceil(UnitConv.mm2pt(297))); //page size A4 (in pt) 96 g2d.setupDocument(out, pageSize.width, pageSize.height); 97 g2d.translate(144, 72); //Establish some page borders 98 99 //A few rectangles rotated and with different color 100 Graphics2D copy = (Graphics2D)g2d.create(); 101 int c = 12; 102 for (int i = 0; i < c; i++) { 103 float f = ((i + 1) / (float)c); 104 Color col = new Color(0.0f, 1 - f, 0.0f); 105 copy.setColor(col); 106 copy.fillRect(70, 90, 50, 50); 107 copy.rotate(-2 * Math.PI / c, 70, 90); 108 } 109 copy.dispose(); 110 111 //Some text 112 g2d.rotate(-0.25); 113 g2d.setColor(Color.RED); 114 g2d.setFont(new Font("sans-serif", Font.PLAIN, 36)); 115 g2d.drawString("Hello world!", 140, 140); 116 g2d.setColor(Color.RED.darker()); 117 g2d.setFont(new Font("serif", Font.PLAIN, 36)); 118 g2d.drawString("Hello world!", 140, 180); 119 120 pageSize = new Dimension(pageSize.height, pageSize.width); 121 g2d.nextPage(pageSize.width, pageSize.height); 122 123 //Demonstrate painting rich text 124 String someHTML = "<html><body style=\"font-family:Verdana\">" 125 + "<p>Welcome to <b>page 2!</b></p>" 126 + "<h2>PDFDocumentGraphics2D Demonstration</h2>" 127 + "<p>We can <i>easily</i> paint some HTML here!</p>" 128 + "<p style=\"color:green;\">" 129 + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin accumsan" 130 + " condimentum ullamcorper. Sed varius quam id arcu fermentum luctus. Praesent" 131 + " nisi ligula, cursus sed vestibulum vel, sodales sed lectus.</p>" 132 + "</body></html>"; 133 JEditorPane htmlComp = new JEditorPane(); 134 htmlComp.setContentType("text/html"); 135 htmlComp.read(new StringReader(someHTML), null); 136 htmlComp.setSize(new Dimension(pageSize.width - 72, pageSize.height - 72)); 137 //htmlComp.setBackground(Color.ORANGE); 138 htmlComp.validate(); 139 htmlComp.printAll(g2d); 140 141 //Cleanup 142 g2d.finish(); 143 } finally { 144 IOUtils.closeQuietly(out); 145 } 146 } 147 148 /** 149 * Main method. 150 * @param args command-line arguments 151 */ main(String[] args)152 public static void main(String[] args) { 153 try { 154 System.out.println("FOP " + ExampleJava2D2PDF.class.getSimpleName() + "\n"); 155 System.out.println("Preparing..."); 156 157 //Setup directories 158 File baseDir = new File("."); 159 File outDir = new File(baseDir, "out"); 160 if (!outDir.isDirectory()) { 161 if (!outDir.mkdirs()) { 162 throw new IOException("Could not create output directory: " + outDir); 163 } 164 } 165 166 //Setup output file 167 File pdffile = new File(outDir, "ResultJava2D2PDF.pdf"); 168 169 System.out.println("Output: PDF (" + pdffile + ")"); 170 System.out.println(); 171 System.out.println("Generating..."); 172 173 ExampleJava2D2PDF app = new ExampleJava2D2PDF(); 174 app.generatePDF(pdffile); 175 176 System.out.println("Success!"); 177 } catch (Throwable t) { 178 t.printStackTrace(System.err); 179 System.exit(-1); 180 } 181 } 182 } 183