use of org.apache.fop.apps.FopFactory in project tutorials by eugenp.
the class ApacheFOPConvertHTMLIntegrationTest method fromFOFileToPDF.
private void fromFOFileToPDF() throws Exception {
final FopFactory fopFactory = FopFactory.newInstance();
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(output_html2fo)));
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
final TransformerFactory factory = TransformerFactory.newInstance();
final Transformer transformer = factory.newTransformer();
final Source src = new StreamSource(new FileInputStream(foFile));
final Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
outStream.close();
}
use of org.apache.fop.apps.FopFactory in project ofbiz-framework by apache.
the class ScreenFopViewHandler method render.
/**
* @see org.apache.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
Delegator delegator = (Delegator) request.getAttribute("delegator");
VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
ModelTheme modelTheme = visualTheme.getModelTheme();
// render and obtain the XSL-FO
Writer writer = new StringWriter();
try {
ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType(getName()), modelTheme.getScreenRendererLocation(getName()));
FormStringRenderer formStringRenderer = new MacroFormRenderer(modelTheme.getFormRendererLocation(getName()), request, response);
// TODO: uncomment these lines when the renderers are implemented
// TreeStringRenderer treeStringRenderer = new MacroTreeRenderer(modelTheme.getTreeRendererLocation(getName()), writer);
// MenuStringRenderer menuStringRenderer = new MacroMenuRenderer(modelTheme.getMenuRendererLocation(getName()), writer);
ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
screens.populateContextForRequest(request, response, servletContext);
// this is the object used to render forms from their definitions
screens.getContext().put("formStringRenderer", formStringRenderer);
screens.getContext().put("simpleEncoder", UtilCodec.getEncoder(modelTheme.getEncoder(getName())));
screens.render(page);
} catch (IOException | GeneralException | SAXException | ParserConfigurationException | TemplateException e) {
renderError("Problems with the response writer/output stream", e, "[Not Yet Rendered]", request, response);
return;
}
// set the input source (XSL-FO) and generate the output stream of contentType
String screenOutString = writer.toString();
if (!screenOutString.startsWith("<?xml")) {
screenOutString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + screenOutString;
}
if (Debug.verboseOn())
Debug.logVerbose("XSL:FO Screen Output: " + screenOutString, module);
if (UtilValidate.isEmpty(contentType)) {
contentType = modelTheme.getContentType(getName());
}
// get encryption related parameters
FOUserAgent foUserAgent = null;
String userPassword = request.getParameter("userPassword");
String ownerPassword = request.getParameter("ownerPassword");
boolean allowPrint = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrint")) ? ApacheFopWorker.getAllowPrintDefault() : request.getParameter("allowPrint"));
boolean allowCopyContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowCopyContent")) ? ApacheFopWorker.getAllowCopyContentDefault() : request.getParameter("allowCopyContent"));
boolean allowEditContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditContent")) ? ApacheFopWorker.getAllowEditContentDefault() : request.getParameter("allowEditContent"));
boolean allowEditAnnotations = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditAnnotations")) ? ApacheFopWorker.getAllowEditAnnotationsDefault() : request.getParameter("allowEditAnnotations"));
if (UtilValidate.isNotEmpty(userPassword) || UtilValidate.isNotEmpty(ownerPassword) || !allowPrint || !allowCopyContent || allowEditContent || !allowEditAnnotations) {
int encryptionLength = 128;
try {
encryptionLength = Integer.parseInt(request.getParameter("encryption-length"));
} catch (NumberFormatException e) {
try {
encryptionLength = Integer.parseInt(ApacheFopWorker.getEncryptionLengthDefault());
} catch (NumberFormatException e1) {
// ignore
}
}
boolean encryptMetadata = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("encrypt-metadata")) ? ApacheFopWorker.getEncryptMetadataDefault() : request.getParameter("encrypt-metadata"));
boolean allowFillInForms = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowFillInForms")) ? ApacheFopWorker.getAllowFillInFormsDefault() : request.getParameter("allowFillInForms"));
boolean allowAccessContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAccessContent")) ? ApacheFopWorker.getAllowAccessContentDefault() : request.getParameter("allowAccessContent"));
boolean allowAssembleDocument = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAssembleDocument")) ? ApacheFopWorker.getAllowAssembleDocumentDefault() : request.getParameter("allowAssembleDocument"));
boolean allowPrintHq = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrintHq")) ? ApacheFopWorker.getAllowPrintHqDefault() : request.getParameter("allowPrintHq"));
FopFactory fopFactory = ApacheFopWorker.getFactoryInstance();
foUserAgent = fopFactory.newFOUserAgent();
PDFEncryptionParams pdfEncryptionParams = new PDFEncryptionParams(userPassword, ownerPassword, allowPrint, allowCopyContent, allowEditContent, allowEditAnnotations, encryptMetadata);
pdfEncryptionParams.setAllowFillInForms(allowFillInForms);
pdfEncryptionParams.setAllowAccessContent(allowAccessContent);
pdfEncryptionParams.setAllowAssembleDocument(allowAssembleDocument);
pdfEncryptionParams.setAllowPrintHq(allowPrintHq);
pdfEncryptionParams.setEncryptionLengthInBits(encryptionLength);
foUserAgent.getRendererOptions().put(PDFEncryptionOption.ENCRYPTION_PARAMS, pdfEncryptionParams);
}
Reader reader = new StringReader(screenOutString);
StreamSource src = new StreamSource(reader);
ByteArrayOutputStream out = new ByteArrayOutputStream();
/* Debug area, uncomment this to view the xml file generate before analyse by fop
try {
java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/temp.xsl.fo"));
fw.write(screenOutString);
fw.close();
} catch (IOException e) {
Debug.logError(e, "Couldn't save xls debug file: " + e.toString(), module);
}
*/
try {
Fop fop = ApacheFopWorker.createFopInstance(out, contentType, foUserAgent);
ApacheFopWorker.transform(src, null, fop);
} catch (Exception e) {
renderError("Unable to transform FO file", e, screenOutString, request, response);
return;
}
// set the content type and length
response.setContentType(contentType);
response.setContentLength(out.size());
// write to the browser
try {
out.writeTo(response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
renderError("Unable to write to OutputStream", e, screenOutString, request, response);
}
}
use of org.apache.fop.apps.FopFactory in project ofbiz-framework by apache.
the class ApacheFopWorker method createFopInstance.
/**
* 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;
}
use of org.apache.fop.apps.FopFactory in project Digital by hneemann.
the class DocuTest method testDocu.
public void testDocu() throws IOException, NodeException, PinException, TransformerException, SAXException {
FopFactory fopFactory = FopFactory.newInstance(new File(Resources.getRoot(), "docu/fop.xconf"));
File maven = Resources.getRoot().getParentFile().getParentFile().getParentFile();
File target = new File(maven, "target/docu");
File target2 = new File(maven, "target/docuDist");
File images = new File(target, "img");
images.mkdirs();
target2.mkdirs();
final File library = new File(target, "library.xml");
write74xx(library);
for (Language l : Lang.getBundle().getSupportedLanguages()) {
// set language
Lang.setActualRuntimeLanguage(l);
final String basename = "Documentation_" + l.getName();
// write xml
File xml = new File(target, basename + ".xml");
try (Writer w = new OutputStreamWriter(new FileOutputStream(xml), "UTF-8")) {
writeXML(w, images, l.getName(), library);
}
// start xslt transformation
File xslFO = new File(target, basename + ".fo");
File xslt = new File(Resources.getRoot(), "docu/elem2fo.xslt");
startXalan(xml, xslt, xslFO);
// write pdf
File pdf = new File(target, basename + ".pdf");
startFOP(fopFactory, xslFO, pdf);
copy(pdf, new File(target2, "Doc_" + l + ".pdf"));
}
}
use of org.apache.fop.apps.FopFactory in project opentheso by miledrousset.
the class ApacheFOP method test_basic.
public void test_basic() throws SAXException, IOException, TransformerConfigurationException, TransformerException, ConfigurationException, URISyntaxException {
// Step 1: Construct a FopFactory by specifying a reference to the configuration file
// (reuse if you plan to render multiple documents!)
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("fop-config.xml"));
URI baseURI = new URI("https://www.testUri.com");
FopFactoryBuilder builder;
builder = new FopFactoryBuilder(baseURI).setConfiguration(cfg);
FopFactory fopFactory = builder.build();
// Step 2: Set up output stream.
// Note: Using BufferedOutputStream for performance reasons (helpful with FileOutputStreams).
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("test-fop.pdf")));
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 4: Setup JAXP using identity transformer
Source xslt = new StreamSource(new File("skos-alpha.xsl"));
TransformerFactory factory = TransformerFactory.newInstance();
// identity transformer
Transformer transformer = factory.newTransformer(xslt);
// Step 5: Setup input and output for XSLT transformation
// Setup input stream
Source src = new StreamSource(new File("test_unesco.rdf"));
// 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);
} finally {
// Clean-up
out.close();
}
}
Aggregations