1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.print.out.pdf;
|
12
|
|
13
|
import java.io.File;
|
14
|
import java.io.FileOutputStream;
|
15
|
import java.io.InputStream;
|
16
|
import java.io.OutputStream;
|
17
|
|
18
|
import javax.xml.transform.Result;
|
19
|
import javax.xml.transform.Transformer;
|
20
|
import javax.xml.transform.TransformerFactory;
|
21
|
import javax.xml.transform.sax.SAXResult;
|
22
|
import javax.xml.transform.stream.StreamSource;
|
23
|
|
24
|
import org.apache.fop.apps.FOUserAgent;
|
25
|
import org.apache.fop.apps.Fop;
|
26
|
import org.apache.fop.apps.FopFactory;
|
27
|
import org.apache.fop.apps.MimeConstants;
|
28
|
import org.apache.log4j.Logger;
|
29
|
import org.jdom.Document;
|
30
|
import org.jdom.transform.JDOMSource;
|
31
|
|
32
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
33
|
import eu.etaxonomy.cdm.print.out.PublishOutputModuleBase;
|
34
|
|
35
|
/**
|
36
|
* This output module will create a <a href="http://www.adobe.com/products/acrobat/adobepdf.html">PDF</a> document
|
37
|
* with a predefined output for quick printing.
|
38
|
*
|
39
|
* @author n.hoffmann
|
40
|
* @created Jul 20, 2010
|
41
|
* @version 1.0
|
42
|
*/
|
43
|
public class PdfOutputModule extends PublishOutputModuleBase {
|
44
|
private static final Logger logger = Logger
|
45
|
.getLogger(PdfOutputModule.class);
|
46
|
|
47
|
public static String STYLESHEET_RESOURCE_DEFAULT = "/stylesheets/pdf/cdmToPdf.xsl";
|
48
|
|
49
|
/*
|
50
|
* (non-Javadoc)
|
51
|
* @see eu.etaxonomy.printpublisher.out.IPublishOutputModule#output(org.jdom.Document, java.io.File, eu.etaxonomy.printpublisher.NotificationMediator)
|
52
|
*/
|
53
|
public void output(Document document, File exportFolder, IProgressMonitor progressMonitor) {
|
54
|
|
55
|
super.output(document, exportFolder, progressMonitor);
|
56
|
|
57
|
try{
|
58
|
|
59
|
// configure fopFactory as desired
|
60
|
FopFactory fopFactory = FopFactory.newInstance();
|
61
|
|
62
|
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
|
63
|
// configure foUserAgent as desired
|
64
|
|
65
|
// Setup output
|
66
|
String filePath = getFilePath(exportFolder);
|
67
|
OutputStream out = new FileOutputStream(filePath);
|
68
|
out = new java.io.BufferedOutputStream(out);
|
69
|
|
70
|
try{
|
71
|
|
72
|
// Construct fop with desired output format
|
73
|
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
|
74
|
|
75
|
// Setup XSLT
|
76
|
InputStream xslt = getXsltInputStream();
|
77
|
TransformerFactory factory = TransformerFactory.newInstance();
|
78
|
Transformer transformer = factory.newTransformer(new StreamSource(xslt));
|
79
|
|
80
|
// Setup input for XSLT transformation
|
81
|
JDOMSource source = new JDOMSource(document);
|
82
|
|
83
|
// Resulting SAX events (the generated FO) must be piped through to FOP
|
84
|
Result result = new SAXResult(fop.getDefaultHandler());
|
85
|
|
86
|
// Start XSLT transformation and FOP processing
|
87
|
transformer.transform(source, result);
|
88
|
} finally {
|
89
|
out.close();
|
90
|
logger.info("PDF file created: " + filePath);
|
91
|
}
|
92
|
|
93
|
}catch (Exception e) {
|
94
|
logger.error("Could not generate document", e);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
public String getOutputFileSuffix() {
|
99
|
return "pdf";
|
100
|
}
|
101
|
|
102
|
public InputStream getDefaultXsltInputStream(){
|
103
|
return PdfOutputModule.class.getResourceAsStream(STYLESHEET_RESOURCE_DEFAULT);
|
104
|
}
|
105
|
}
|