forked from Coded/SIPRP
				
			git-svn-id: https://svn.coded.pt/svn/SIPRP@998 bb69d46d-e84e-40c8-a05a-06db0d633741
	
		
	
				
					
				
			
							parent
							
								
									9e95aae1cc
								
							
						
					
					
						commit
						857c1f6a39
					
				| @ -0,0 +1,190 @@ | |||||||
|  | /* | ||||||
|  |  * PDFCreator.java | ||||||
|  |  * | ||||||
|  |  * Created on 19 de Abril de 2006, 18:20 | ||||||
|  |  * | ||||||
|  |  * To change this template, choose Tools | Template Manager | ||||||
|  |  * and open the template in the editor. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | package siprp.fop; | ||||||
|  | 
 | ||||||
|  | import java.io.ByteArrayInputStream; | ||||||
|  | import java.io.ByteArrayOutputStream; | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.FileInputStream; | ||||||
|  | import java.io.FileNotFoundException; | ||||||
|  | import java.io.FileOutputStream; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.io.OutputStream; | ||||||
|  | 
 | ||||||
|  | import javax.swing.JFileChooser; | ||||||
|  | import javax.xml.transform.Result; | ||||||
|  | import javax.xml.transform.Source; | ||||||
|  | import javax.xml.transform.Transformer; | ||||||
|  | import javax.xml.transform.TransformerFactory; | ||||||
|  | import javax.xml.transform.sax.SAXResult; | ||||||
|  | import javax.xml.transform.stream.StreamSource; | ||||||
|  | 
 | ||||||
|  | import org.apache.avalon.framework.logger.ConsoleLogger; | ||||||
|  | import org.apache.fop.apps.Driver; | ||||||
|  | import org.apache.fop.apps.Options; | ||||||
|  | import org.apache.fop.viewer.SecureResourceBundle; | ||||||
|  | import org.apache.fop.viewer.Translator; | ||||||
|  | 
 | ||||||
|  | import com.evolute.utils.fop.FOPCreator; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * PDFCreator creates PDF's using FO. | ||||||
|  |  * | ||||||
|  |  * @author      Frederico Palma | ||||||
|  |  * @version     %I%, %G% | ||||||
|  |  */ | ||||||
|  | public class PDFCreator | ||||||
|  | { | ||||||
|  | 	private final Translator translator = new SecureResourceBundle( null ); | ||||||
|  | 
 | ||||||
|  | 	private static final Object LOCK = new Object(); | ||||||
|  | 	 | ||||||
|  | 	private static PDFCreator pdfCreator; | ||||||
|  | 
 | ||||||
|  | 	private static String userConfig; | ||||||
|  | 	 | ||||||
|  | 	public static PDFCreator getPDFCreator() | ||||||
|  | 	{ | ||||||
|  | 		synchronized( LOCK ) | ||||||
|  | 		{ | ||||||
|  | 			if( pdfCreator == null ) | ||||||
|  | 			{ | ||||||
|  | 				pdfCreator = new PDFCreator(); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return pdfCreator; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public static void setUserConfig(String string) | ||||||
|  | 	{ | ||||||
|  | 		PDFCreator.userConfig = string; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public static void main( String arg[] ) | ||||||
|  | 		throws Exception | ||||||
|  | 	{ | ||||||
|  | 		final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||||||
|  | 		final FOPCreator creator = FOPCreator.getFOPCreator(); | ||||||
|  | 		System.out.println( "Creating FO" ); | ||||||
|  | 		 | ||||||
|  | 		JFileChooser chooser = new JFileChooser(); | ||||||
|  | 		chooser.setDialogTitle( "Select XML data file:" ); | ||||||
|  | 	    int returnVal = chooser.showOpenDialog( null ); | ||||||
|  | 		String dataFile = null; | ||||||
|  | 		if( returnVal == JFileChooser.APPROVE_OPTION ) | ||||||
|  | 		{ | ||||||
|  | 			dataFile = chooser.getSelectedFile().getAbsolutePath(); | ||||||
|  | 		} | ||||||
|  | 		chooser.setDialogTitle( "Select XSL-FO template file:" ); | ||||||
|  | 		returnVal = chooser.showOpenDialog( null ); | ||||||
|  | 		String templateFile = null; | ||||||
|  | 		if( returnVal == JFileChooser.APPROVE_OPTION ) | ||||||
|  | 		{ | ||||||
|  | 			templateFile = chooser.getSelectedFile().getAbsolutePath(); | ||||||
|  | System.out.println( chooser.getSelectedFile().getAbsolutePath() ); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		final String DATA = dataFile; | ||||||
|  | 		final String TEMPLATE = templateFile; | ||||||
|  | 		creator.createFOfromXML( new FileInputStream( DATA ),  | ||||||
|  | 								new FileInputStream( TEMPLATE ), baos ); | ||||||
|  | //		Thread t = new Thread() {
 | ||||||
|  | //				public void run()
 | ||||||
|  | //				{
 | ||||||
|  | //					try
 | ||||||
|  | //					{
 | ||||||
|  | //						creator.createFOfromXML( new FileInputStream( DATA ), 
 | ||||||
|  | //								new FileInputStream( TEMPLATE ), baos );
 | ||||||
|  | //						System.out.println( "FO created" );
 | ||||||
|  | //					}
 | ||||||
|  | //					catch( Exception ex )
 | ||||||
|  | //					{
 | ||||||
|  | //						ex.printStackTrace();
 | ||||||
|  | //					}
 | ||||||
|  | //				} };
 | ||||||
|  | //		t.start();
 | ||||||
|  | //		Thread.currentThread().sleep( 2 );
 | ||||||
|  | 		System.out.println( "Starting to print" ); | ||||||
|  | //		options.put( FOP_DUPLEX_TYPE, javax.print.attribute.standard.Sides.TUMBLE );
 | ||||||
|  | 		byte pdf[]  = PDFCreator.getPDFCreator().createPdfFromFo( baos.toByteArray() ); | ||||||
|  | //		new FOPPrinter().printFO( new FileInputStream( "c:\\simplecol.fo" ), true, false );
 | ||||||
|  | 		File pdfFile = new File( "/home/fpalma/acss/teste.pdf" ); | ||||||
|  | 		File foFile = new File( "/home/fpalma/Desktop/teste.fo" ); | ||||||
|  | 		pdfFile.createNewFile(); | ||||||
|  | 		foFile.createNewFile(); | ||||||
|  | 		FileOutputStream fos = new FileOutputStream( pdfFile ); | ||||||
|  | 		FileOutputStream fofos = new FileOutputStream( foFile ); | ||||||
|  | 		fos.write( pdf ); | ||||||
|  | 		fofos.write( baos.toByteArray() ); | ||||||
|  | 		fos.close(); | ||||||
|  | 		fofos.close(); | ||||||
|  | 		System.out.println( "DONE" ); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	/** Creates a new instance of PDFCreator */ | ||||||
|  | 	private PDFCreator() | ||||||
|  | 	{ | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Creates PDF content as an array of bytes, given FO content as an array of bytes. | ||||||
|  | 	 * | ||||||
|  | 	 * @param fo  the byte[] with the FO content to convert | ||||||
|  | 	 * @return the PDF content | ||||||
|  | 	 * @throws Exception  if the conversion fails | ||||||
|  | 	 * | ||||||
|  | 	 */ | ||||||
|  | 	public byte[]createPdfFromFo( byte []fo ) | ||||||
|  | 		throws Exception | ||||||
|  | 	{ | ||||||
|  | 		 | ||||||
|  | 		InputStream inputStreamConf = ClassLoader.getSystemClassLoader().getResourceAsStream("siprp/fop/fop.xml"); | ||||||
|  | 		InputStream inputStreamDTD = ClassLoader.getSystemClassLoader().getResourceAsStream("siprp/fop/config.dtd"); | ||||||
|  | 		 | ||||||
|  | 		/*************************/ | ||||||
|  | 		 | ||||||
|  | 		File f = File.createTempFile("fop", "conf"); | ||||||
|  | 		f.deleteOnExit(); | ||||||
|  | 		writeFile(inputStreamConf, f); | ||||||
|  | 		 | ||||||
|  | 		System.out.println(f.getParentFile()); | ||||||
|  | 		 | ||||||
|  | 		/*************************/ | ||||||
|  | 		File dtd = new File(f.getParentFile(), "config.dtd"); | ||||||
|  | 		writeFile(inputStreamDTD, dtd); | ||||||
|  | 		 | ||||||
|  | 		new Options(f); | ||||||
|  | 		 | ||||||
|  | 		ByteArrayOutputStream pdf = new ByteArrayOutputStream(); | ||||||
|  | 		TransformerFactory factory = TransformerFactory.newInstance(); | ||||||
|  | 		Transformer transformer = factory.newTransformer(); | ||||||
|  | 		Source src = new StreamSource( new ByteArrayInputStream( fo ) ); | ||||||
|  | 		Driver driver = new Driver(); | ||||||
|  |         driver.setLogger( new ConsoleLogger( ConsoleLogger.LEVEL_INFO ) ); | ||||||
|  |         driver.setRenderer( Driver.RENDER_PDF ); | ||||||
|  | 		driver.setOutputStream( pdf ); | ||||||
|  | 		Result res = new SAXResult( driver.getContentHandler() ); | ||||||
|  | 		transformer.transform( src, res ); | ||||||
|  | 		return pdf.toByteArray(); | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	private void writeFile(InputStream inputStreamConf, File f) | ||||||
|  | 			throws FileNotFoundException, IOException { | ||||||
|  | 		OutputStream out = new FileOutputStream(f); | ||||||
|  | 		byte buf[] = new byte[1024]; | ||||||
|  | 		int len; | ||||||
|  | 		while ((len = inputStreamConf.read(buf)) > 0) | ||||||
|  | 			out.write(buf, 0, len); | ||||||
|  | 		out.close(); | ||||||
|  | 		inputStreamConf.close(); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -0,0 +1,22 @@ | |||||||
|  | <!ELEMENT configuration (entry*, fonts?)> | ||||||
|  | <!ELEMENT entry (key,(value | list))> | ||||||
|  | <!ATTLIST entry | ||||||
|  | 	role CDATA #IMPLIED | ||||||
|  | > | ||||||
|  | <!ELEMENT key (#PCDATA)> | ||||||
|  | <!ELEMENT value (#PCDATA)> | ||||||
|  | <!ELEMENT list (value+ | subentry+)> | ||||||
|  | <!ELEMENT subentry (key,value+)> | ||||||
|  | <!ELEMENT fonts (font*)> | ||||||
|  | <!ELEMENT font (font-triplet+)> | ||||||
|  | <!ATTLIST font | ||||||
|  | 	metrics-file CDATA #REQUIRED | ||||||
|  | 	kerning	CDATA #IMPLIED | ||||||
|  | 	embed-file CDATA #REQUIRED | ||||||
|  | > | ||||||
|  | <!ELEMENT font-triplet EMPTY> | ||||||
|  | <!ATTLIST font-triplet | ||||||
|  | 	name CDATA #REQUIRED | ||||||
|  | 	style CDATA #REQUIRED | ||||||
|  | 	weight CDATA #REQUIRED | ||||||
|  | > | ||||||
| @ -0,0 +1,89 @@ | |||||||
|  | <!DOCTYPE configuration SYSTEM "config.dtd"> | ||||||
|  | <!--  | ||||||
|  |      this file contains templates which allow an user easy  | ||||||
|  |      configuration of Fop. Actually normally you don't need this configuration  | ||||||
|  |      file, but if you need to change configuration, you should | ||||||
|  |      always use this file and *not* config.xml.  | ||||||
|  |      Usage: java org.apache.fop.apps.Fop -c userconfig.xml -fo fo-file -pdf pdf-file | ||||||
|  | --> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | <configuration> | ||||||
|  | 
 | ||||||
|  | <!--  NOT IMPLEMENTED | ||||||
|  | basedir: normally the base directory is the directory where the fo file is  | ||||||
|  |          located. if you want to specify your own, uncomment this entry | ||||||
|  | --> | ||||||
|  | <!--  | ||||||
|  |   <entry> | ||||||
|  |     <key>baseDir</key> | ||||||
|  |     <value></value> | ||||||
|  |   </entry> | ||||||
|  | --> | ||||||
|  | 
 | ||||||
|  | <!-- | ||||||
|  | ************************************************************************ | ||||||
|  |                         HYPHENATION  | ||||||
|  | ************************************************************************ | ||||||
|  | --> | ||||||
|  |    | ||||||
|  | <!-- | ||||||
|  |    hyphenation directory  | ||||||
|  |    if you want to specify your own directory with hyphenation pattern | ||||||
|  |    then uncomment the next entry and add the directory name | ||||||
|  | --> | ||||||
|  | 
 | ||||||
|  | <!-- | ||||||
|  |   <entry> | ||||||
|  |     <key>hyphenation-dir</key> | ||||||
|  |     <value>/java/xml-fop/hyph</value> | ||||||
|  |   </entry> | ||||||
|  | --> | ||||||
|  | 
 | ||||||
|  | <!-- | ||||||
|  | ************************************************************************ | ||||||
|  |   Add fonts here | ||||||
|  | ************************************************************************ | ||||||
|  | --> | ||||||
|  | 
 | ||||||
|  | <fonts> | ||||||
|  |  <!-- example --> | ||||||
|  |  <!-- | ||||||
|  |  <font metrics-file="arial.xml" kerning="yes" embed-file="arial.ttf"> | ||||||
|  |     <font-triplet name="Arial" style="normal" weight="normal"/> | ||||||
|  |     <font-triplet name="ArialMT" style="normal" weight="normal"/> | ||||||
|  |  </font> | ||||||
|  |  <font metrics-file="arialb.xml" kerning="yes" embed-file="arialb.ttf"> | ||||||
|  |     <font-triplet name="Arial" style="normal" weight="bold"/> | ||||||
|  |     <font-triplet name="ArialMT" style="normal" weight="bold"/> | ||||||
|  |  </font> | ||||||
|  |  <font metrics-file="ariali.xml" kerning="yes" embed-file="ariali.ttf"> | ||||||
|  |     <font-triplet name="Arial" style="italic" weight="normal"/> | ||||||
|  |     <font-triplet name="ArialMT" style="italic" weight="normal"/> | ||||||
|  |  </font> | ||||||
|  |  <font metrics-file="arialbi.xml" kerning="yes" embed-file="arialbi.ttf"> | ||||||
|  |     <font-triplet name="Arial" style="italic" weight="bold"/> | ||||||
|  |     <font-triplet name="ArialMT" style="italic" weight="bold"/> | ||||||
|  |  </font> | ||||||
|  |  --> | ||||||
|  |  <!-- Example Japanese fonts | ||||||
|  |  <font metrics-file="msgothic.xml" embed-file="D:\winnt\font\msgothic.ttc" kerning="yes"> | ||||||
|  |     <font-triplet name="Gothic" style="normal" weight="normal"/> | ||||||
|  |     <font-triplet name="Gothic" style="normal" weight="bold"/> | ||||||
|  |     <font-triplet name="Gothic" style="italic" weight="normal"/> | ||||||
|  |     <font-triplet name="Gothic" style="italic" weight="bold"/> | ||||||
|  |  </font> | ||||||
|  |  <font metrics-file="msmincho.xml" embed-file="Cyberbit.ttf" kerning="yes"> | ||||||
|  |     <font-triplet name="Mincho" style="normal" weight="normal"/> | ||||||
|  |     <font-triplet name="Mincho" style="normal" weight="bold"/> | ||||||
|  |     <font-triplet name="Mincho" style="italic" weight="normal"/> | ||||||
|  |     <font-triplet name="Mincho" style="italic" weight="bold"/> | ||||||
|  |    </font> | ||||||
|  |   --> | ||||||
|  | </fonts> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | </configuration> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
					Loading…
					
					
				
		Reference in new issue