org.apache.fop.apps.FOUserAgent Java Examples

The following examples show how to use org.apache.fop.apps.FOUserAgent. 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 7 votes vote down vote up
/** Returns a new Fop instance. Note: FOP documentation recommends using
 * a Fop instance for one transform run only.
 * @param out The target (result) OutputStream instance
 * @param outputFormat Optional output format, defaults to "application/pdf"
 * @param foUserAgent FOUserAgent object which may contains encryption-params in render options
 * @return Fop instance
 */
public static Fop createFopInstance(OutputStream out, String outputFormat, FOUserAgent foUserAgent) throws FOPException {
    if (UtilValidate.isEmpty(outputFormat)) {
        outputFormat = MimeConstants.MIME_PDF;
    }
    if (UtilValidate.isEmpty(foUserAgent)) {
        FopFactory fopFactory = getFactoryInstance();
        foUserAgent = fopFactory.newFOUserAgent();
    }
    Fop fop;
    if (out != null) {
        fop = fopFactory.newFop(outputFormat, foUserAgent, out);
    } else {
        fop = fopFactory.newFop(outputFormat, foUserAgent);
    }
    return fop;
}
 
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: PrintPreviewDialog.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected AWTRenderer doInBackground() throws Exception
{
	URI osPath = new File(ConfigurationSettings.getOutputSheetsDir()).toURI();
	File xsltFile = new File(osPath.resolve(uri));

	FOUserAgent userAgent = FopTask.getFactory().newFOUserAgent();
	AWTRenderer renderer = new AWTRenderer(userAgent, null, false, false);
	PipedOutputStream out = new PipedOutputStream();
	FopTask task = FopTask.newFopTask(new PipedInputStream(out), xsltFile, renderer);
	Thread thread = new Thread(task, "fop-preview");
	thread.setDaemon(true);
	thread.start();
	BatchExporter.exportCharacter(character, out);
	try
	{
		thread.join();
	}
	catch (InterruptedException ex)
	{
		//pass on the interrupt and hope it stops
		thread.interrupt();
	}
	return renderer;
}
 
Example #5
Source File: PrintPreviewDialog.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected AWTRenderer doInBackground() throws Exception
{
	URI osPath = new File(ConfigurationSettings.getOutputSheetsDir()).toURI();
	File xsltFile = new File(osPath.resolve(uri));

	FOUserAgent userAgent = FopTask.getFactory().newFOUserAgent();
	AWTRenderer renderer = new AWTRenderer(userAgent, null, false, false);
	PipedOutputStream out = new PipedOutputStream();
	FopTask task = FopTask.newFopTask(new PipedInputStream(out), xsltFile, renderer);
	Thread thread = new Thread(task, "fop-preview");
	thread.setDaemon(true);
	thread.start();
	BatchExporter.exportCharacter(character, out);
	try
	{
		thread.join();
	}
	catch (InterruptedException ex)
	{
		//pass on the interrupt and hope it stops
		thread.interrupt();
	}
	return renderer;
}
 
Example #6
Source File: FORendererApacheFOP.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
/**
 * Allow user access to FOUserAgent, so they can setAccessibility(true).  Access to other settings
 * is possible but unsupported.
 * 
 * @param wmlPackage
 * @return
 * @throws FOPException
 */
public static FOUserAgent getFOUserAgent(FOSettings settings) throws Docx4JException, FOPException {
	
	FopFactory fopFactory = getFopFactory(
			setupApacheFopConfiguration(settings)); // relies on the WordML package being there, for font info
	settings.getSettings().put(FOP_FACTORY, fopFactory);
	
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
	settings.getSettings().put(FO_USER_AGENT, foUserAgent);
	
	return foUserAgent;
}
 
Example #7
Source File: SiteInfoToolServlet.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Takes a DOM structure and renders a PDF
 * 
 * @param doc
 *        DOM structure
 * @param streamOut
 */
@SuppressWarnings("unchecked")
protected void generatePDF(Document doc, OutputStream streamOut)
{
	String xslFileName = "participants-all-attrs.xsl";
	Locale currentLocale = rb.getLocale();
	if (currentLocale!=null){
		String fullLocale = currentLocale.toString();
		xslFileName = "participants-all-attrs_" + fullLocale + ".xsl";
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
		if (inputStream == null){
			xslFileName = "participants-all-attrs_" + currentLocale.getCountry() + ".xsl";
			inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
			if (inputStream == null){
				//We use the default file
				xslFileName = "participants-all-attrs.xsl";
			}
		}

		IOUtils.closeQuietly(inputStream);
	}
	String configFileName = "userconfig.xml";
	DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
	InputStream configInputStream = null;
	try 
	{
		configInputStream = getClass().getClassLoader().getResourceAsStream(configFileName);
		Configuration cfg = cfgBuilder.build(configInputStream);
		
		FopFactory fopFactory = FopFactory.newInstance();
		fopFactory.setUserConfig(cfg);
		fopFactory.setStrictValidation(false);
		FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
		if (!StringUtils.isEmpty(ServerConfigurationService.getString("pdf.default.font"))) {
		    // this allows font substitution to support i18n chars in PDFs - SAK-21909
		    FontQualifier fromQualifier = new FontQualifier();
		    fromQualifier.setFontFamily("DEFAULT_FONT");
		    FontQualifier toQualifier = new FontQualifier();
		    toQualifier.setFontFamily(ServerConfigurationService.getString("pdf.default.font", "Helvetica"));
		    FontSubstitutions result = new FontSubstitutions();
		    result.add(new FontSubstitution(fromQualifier, toQualifier));
		    fopFactory.getFontManager().setFontSubstitutions(result);
		}
		Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, streamOut);
		InputStream in = getClass().getClassLoader().getResourceAsStream(xslFileName);
		Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
		transformer.setParameter("titleName", rb.getString("sitegen.siteinfolist.title.name"));
		transformer.setParameter("titleId", rb.getString("sitegen.siteinfolist.title.id"));
		transformer.setParameter("titleSection", rb.getString("sitegen.siteinfolist.title.section"));
		transformer.setParameter("titleCredit", rb.getString("sitegen.siteinfolist.title.credit"));
		transformer.setParameter("titleRole", rb.getString("sitegen.siteinfolist.title.role"));
		transformer.setParameter("titleStatus", rb.getString("sitegen.siteinfolist.title.status"));

		Source src = new DOMSource(doc);
		transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
	}
	catch (Exception e)
	{
		log.warn("{}.generatePDF(): {}", this, e.toString());
	}
	finally
	{
		IOUtils.closeQuietly(configInputStream);
	}
}
 
Example #8
Source File: SiteInfoToolServlet.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Takes a DOM structure and renders a PDF
 * 
 * @param doc
 *        DOM structure
 * @param streamOut
 */
@SuppressWarnings("unchecked")
protected void generatePDF(Document doc, OutputStream streamOut)
{
	String xslFileName = "participants-all-attrs.xsl";
	Locale currentLocale = rb.getLocale();
	if (currentLocale!=null){
		String fullLocale = currentLocale.toString();
		xslFileName = "participants-all-attrs_" + fullLocale + ".xsl";
		InputStream inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
		if (inputStream == null){
			xslFileName = "participants-all-attrs_" + currentLocale.getCountry() + ".xsl";
			inputStream = getClass().getClassLoader().getResourceAsStream(xslFileName);
			if (inputStream == null){
				//We use the default file
				xslFileName = "participants-all-attrs.xsl";
			}
		}

		IOUtils.closeQuietly(inputStream);
	}
	String configFileName = "userconfig.xml";
	DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
	InputStream configInputStream = null;
	try 
	{
		configInputStream = getClass().getClassLoader().getResourceAsStream(configFileName);
		Configuration cfg = cfgBuilder.build(configInputStream);
		
		FopFactory fopFactory = FopFactory.newInstance();
		fopFactory.setUserConfig(cfg);
		fopFactory.setStrictValidation(false);
		FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
		if (!StringUtils.isEmpty(ServerConfigurationService.getString("pdf.default.font"))) {
		    // this allows font substitution to support i18n chars in PDFs - SAK-21909
		    FontQualifier fromQualifier = new FontQualifier();
		    fromQualifier.setFontFamily("DEFAULT_FONT");
		    FontQualifier toQualifier = new FontQualifier();
		    toQualifier.setFontFamily(ServerConfigurationService.getString("pdf.default.font", "Helvetica"));
		    FontSubstitutions result = new FontSubstitutions();
		    result.add(new FontSubstitution(fromQualifier, toQualifier));
		    fopFactory.getFontManager().setFontSubstitutions(result);
		}
		Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, streamOut);
		InputStream in = getClass().getClassLoader().getResourceAsStream(xslFileName);
		Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
		transformer.setParameter("titleName", rb.getString("sitegen.siteinfolist.title.name"));
		transformer.setParameter("titleId", rb.getString("sitegen.siteinfolist.title.id"));
		transformer.setParameter("titleSection", rb.getString("sitegen.siteinfolist.title.section"));
		transformer.setParameter("titleCredit", rb.getString("sitegen.siteinfolist.title.credit"));
		transformer.setParameter("titleRole", rb.getString("sitegen.siteinfolist.title.role"));
		transformer.setParameter("titleStatus", rb.getString("sitegen.siteinfolist.title.status"));

		Source src = new DOMSource(doc);
		transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
	}
	catch (Exception e)
	{
		log.warn("{}.generatePDF(): {}", this, e.toString());
	}
	finally
	{
		IOUtils.closeQuietly(configInputStream);
	}
}