Java Code Examples for org.apache.fop.apps.Fop#getDefaultHandler()

The following examples show how to use org.apache.fop.apps.Fop#getDefaultHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApacheFopWorker.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/** Transform an xsl-fo StreamSource to the specified output format.
 * @param src The xsl-fo StreamSource instance
 * @param stylesheet Optional stylesheet StreamSource instance
 * @param fop
 */
public static void transform(StreamSource src, StreamSource stylesheet, Fop fop) throws FOPException {
    Result res = new SAXResult(fop.getDefaultHandler());
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        if (stylesheet == null) {
            transformer = factory.newTransformer();
        } else {
            transformer = factory.newTransformer(stylesheet);
        }
        transformer.setURIResolver(new LocalResolver(transformer.getURIResolver()));
        transformer.transform(src, res);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
 
Example 2
Source File: MCRFoFormatterFOP.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void transform(MCRContent input, OutputStream out) throws TransformerException, IOException {
    try {
        final FOUserAgent userAgent = fopFactory.newFOUserAgent();
        userAgent.setProducer(new MessageFormat("MyCoRe {0} ({1})", Locale.ROOT)
            .format(new Object[] { MCRCoreVersion.getCompleteVersion(), userAgent.getProducer() }));

        final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
        final Source src = input.getSource();
        final Result res = new SAXResult(fop.getDefaultHandler());
        Transformer transformer = getTransformerFactory().newTransformer();
        transformer.transform(src, res);
    } catch (FOPException e) {
        throw new TransformerException(e);
    } finally {
        out.close();
    }
}
 
Example 3
Source File: FORendererApacheFOP.java    From docx4j-export-FO with Apache License 2.0 6 votes vote down vote up
protected void render(FopFactory fopFactory, FOUserAgent foUserAgent, String outputFormat, Source foDocumentSrc, PlaceholderReplacementHandler.PlaceholderLookup placeholderLookup, OutputStream outputStream) throws Docx4JException {
	Fop fop = null;
	Result result = null;
	try {
		if (foUserAgent==null) {
			fop = fopFactory.newFop(outputFormat, outputStream);
		} else {
			fop = fopFactory.newFop(outputFormat, foUserAgent, outputStream);				
		}
		result = (placeholderLookup == null ?
				//1 Pass
				new SAXResult(fop.getDefaultHandler()) :
				//2 Pass
				new SAXResult(new PlaceholderReplacementHandler(fop.getDefaultHandler(), placeholderLookup)));
	} catch (FOPException e) {
		throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
	}
	
	XmlSerializerUtil.serialize(foDocumentSrc, result, false, false);
}
 
Example 4
Source File: DocuTest.java    From Digital with GNU General Public License v3.0 6 votes vote down vote up
private void startFOP(FopFactory fopFactory, File xslfo, File pdfOut) throws IOException, TransformerException, FOPException {
    try (OutputStream out = new BufferedOutputStream(new FileOutputStream(pdfOut))) {
        // Step 3: Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

        // Step 4: Setup JAXP using identity transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer

        // Step 5: Setup input and output for XSLT transformation
        // Setup input stream
        Source src = new StreamSource(xslfo);

        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        // Step 6: Start XSLT transformation and FOP processing
        transformer.transform(src, res);

    }
}
 
Example 5
Source File: PDFGenerationTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void createAndValidate(String filename) throws Exception {
	SimpleCertificateReportFacade facade = SimpleCertificateReportFacade.newFacade();

	File file = new File("src/test/resources/" + filename);
	XmlSimpleCertificateReport simpleReport = facade.unmarshall(file);

	try (FileOutputStream fos = new FileOutputStream("target/report.pdf")) {
		Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, fos);
		Result result = new SAXResult(fop.getDefaultHandler());
		facade.generatePdfReport(simpleReport, result);
	}
	
	File pdfReport = new File("target/report.pdf");
	assertTrue(pdfReport.exists());
	assertTrue(pdfReport.delete(), "Cannot delete PDF document (IO error)");
	assertFalse(pdfReport.exists());
}
 
Example 6
Source File: PDFGenerationTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void createAndValidate(String filename) throws Exception {
	DetailedReportFacade facade = DetailedReportFacade.newFacade();

	File file = new File("src/test/resources/" + filename);
	XmlDetailedReport detailedReport = facade.unmarshall(file);

	try (FileOutputStream fos = new FileOutputStream("target/report.pdf")) {

		Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, fos);
		Result result = new SAXResult(fop.getDefaultHandler());
		facade.generatePdfReport(detailedReport, result);
	}
	
	File pdfReport = new File("target/report.pdf");
	assertTrue(pdfReport.exists());
	assertTrue(pdfReport.delete(), "Cannot delete PDF document (IO error)");
	assertFalse(pdfReport.exists());
	
}
 
Example 7
Source File: PDFGenerationTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void createAndValidate(String filename) throws Exception {
	SimpleReportFacade facade = SimpleReportFacade.newFacade();

	File file = new File("src/test/resources/" + filename);
	XmlSimpleReport simpleReport = facade.unmarshall(file);

	try (FileOutputStream fos = new FileOutputStream("target/report.pdf")) {
		Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, fos);
		Result result = new SAXResult(fop.getDefaultHandler());
		facade.generatePdfReport(simpleReport, result);
	}
	
	File pdfReport = new File("target/report.pdf");
	assertTrue(pdfReport.exists());
	assertTrue(pdfReport.delete(), "Cannot delete PDF document (IO error)");
	assertFalse(pdfReport.exists());
}
 
Example 8
Source File: FopSerializer.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void setOutputStream(final OutputStream outputStream) {
    try {
        Fop fop = FOP_FACTORY.newFop(this.outputFormat, outputStream);
        ContentHandler fopContentHandler = fop.getDefaultHandler();

        this.setContentHandler(fopContentHandler);
    } catch (FOPException e) {
        throw new ProcessingException("Impossible to initialize FOPSerializer", e);
    }
}
 
Example 9
Source File: RendererManagerFopTest.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that a FOP file is valid and transform it into pdf file.
 * @param fopFile the fop content
 * @param pdffile the output
 * @return true if it is valid, false otherwise
 * @throws Exception
 */
private boolean validateFop2pdf( File fopFile, File pdffile ) throws Exception {

	InputStream conf = FopRenderer.class.getResourceAsStream( "/fop.xconf" );
	File fopConfig = new File( this.outputDir, "fop.xconf" );
	Utils.copyStream( conf, fopConfig );

	OutputStream out = null;
	try {
			out = new BufferedOutputStream(new FileOutputStream(pdffile));
			FopFactory fopFactory = FopFactory.newInstance(fopConfig);
			Fop fop =  fopFactory.newFop("application/pdf", out);
			Source src = new StreamSource( fopFile );

			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();

			Result res = new SAXResult(fop.getDefaultHandler());
			transformer.transform(src, res);

	} finally {
			Utils.closeQuietly ( out );
	}

	return pdffile.length() > 0;

}
 
Example 10
Source File: PdfRenderer.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Override
protected File writeFileContent( String fileContent ) throws IOException {

	// Generate FOP rendering
	new RenderingManager().render(
			this.outputDirectory,
			this.applicationTemplate,
			this.applicationDirectory,
			Renderer.FOP,
			this.options,
			this.typeAnnotations );

	File index_fo = new File( this.outputDirectory, "index.fo" );
	File index_pdf = new File( this.outputDirectory, "index.pdf" );

	// Copy the FOP configuration file in outputDirectory
	InputStream conf = getClass().getResourceAsStream( "/fop.xconf" );
	File fopConfig = new File( this.outputDirectory, "fop.xconf" );
	Utils.copyStream( conf, fopConfig );

	// Generate the PDF rendering
	OutputStream out = null;
	try {
		out = new BufferedOutputStream( new FileOutputStream(index_pdf) );
		FopFactory fopFactory = FopFactory.newInstance( fopConfig );
		Fop fop =  fopFactory.newFop( "application/pdf", out );
		Source src = new StreamSource( index_fo );

		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer();

		Result res = new SAXResult(fop.getDefaultHandler());
		transformer.transform(src, res);

	} catch ( Exception e) {
		throw new IOException( e );

	} finally {
		Utils.closeQuietly ( out );
	}

	return index_pdf;
}