Rev 1306 |
Rev 1563 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package jomm.utils;
import org.apache.fop.apps.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;
/**
* User: ngraca
* Date: 2/4/11
* Time: 12:51 PM
*/
public class PdfUtils
{
// public static void createPdfFromXml(File xmlFile, File xslFile, File pdfFile) throws IOException, FOPException, TransformerException {
// createPdfFromXml(xmlFile, xslFile, pdfFile, null);
// }
// public static void createPdfFromXml(File xmlFile, File xslFile, File pdfFile, Map<String, Object> params) throws IOException, FOPException, TransformerException {
// OutputStream out = null;
// try {
// out = new FileOutputStream(pdfFile);
// createPdfFromXml(new StreamSource(xmlFile), xslFile.getAbsolutePath(), out, params);
// }
// finally {
// assert out != null;
// out.close();
// }
// }
public static void createPdfFromXml
(1.5.0/docs/api/javax/xml/transform/Source.html">Source xmlFile,
1.5.0/docs/api/java/lang/String.html">String xslFile,
5+0%2Fdocs%2Fapi+OutputStream">OutputStream pdfFile
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, FOPException,
1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException {
createPdfFromXml
(xmlFile, xslFile, pdfFile,
null);
}
// public static void createPdfFromXml(Source xmlFile, String xslFile, File pdfFile) throws IOException, FOPException, TransformerException {
// OutputStream out = null;
// try {
// out = new FileOutputStream(pdfFile);
// createPdfFromXml(xmlFile, xslFile, out, null);
// }
// finally {
// assert out != null;
// out.close();
// }
// }
//
//
private static 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory transFact =
1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.
newInstance();
private static Map
<String, Templates
> templates =
new Hashtable
<String, Templates
>();
private static 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer getTransformer
(1.5.0/docs/api/java/lang/String.html">String xsltFile,
boolean cache
) throws 1.5.0/docs/api/javax/xml/transform/TransformerConfigurationException.html">TransformerConfigurationException,
1.5.0/docs/api/java/io/IOException.html">IOException {
1.5.0/docs/api/javax/xml/transform/Templates.html">Templates tpl =
null;
if (cache
)
tpl = templates.
get(xsltFile
);
if (tpl ==
null) {
// System.out.println("PdfUtils: Entrou no getTransformer");
1.5.0/docs/api/java/net/URL.html">URL url =
1.5.0/docs/api/java/lang/Thread.html">Thread.
currentThread().
getContextClassLoader().
getResource(xsltFile
);
1.5.0/docs/api/javax/xml/transform/Source.html">Source xsltSource
;
if(xsltFile.
indexOf("http://")>=0 || xsltFile.
indexOf("ftp://")>=0 || xsltFile.
indexOf("https://")>= 0
)
xsltSource =
new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(new 1.5.0/docs/api/java/io/File.html">File(url.
getPath()));
else
xsltSource =
new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(new 1.5.0/docs/api/java/io/File.html">File( url.
getPath().
replace("%20",
" ")));
tpl = transFact.
newTemplates(xsltSource
);
templates.
put(xsltFile, tpl
);
}
return tpl.
newTransformer();
}
static FopFactory fopFactory = FopFactory.
newInstance();
static FOUserAgent foUserAgent = fopFactory.
newFOUserAgent();
// configure foUserAgent as desired
public static void createPdfFromXml
(1.5.0/docs/api/javax/xml/transform/Source.html">Source xmlFile,
1.5.0/docs/api/java/lang/String.html">String xslFile,
5+0%2Fdocs%2Fapi+OutputStream">OutputStream pdfFile, Map
<String, Object
> params
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, FOPException,
1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException {
// configure fopFactory as desired
// Setup output
5+0%2Fdocs%2Fapi+OutputStream">OutputStream out =
new 1.5.0/docs/api/java/io/BufferedOutputStream.html">BufferedOutputStream(pdfFile
);
// Construct fop with desired output format
Fop fop = fopFactory.
newFop(MimeConstants.
MIME_PDF, foUserAgent, out
);
// Setup XSLT
1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = getTransformer
(xslFile,
true);
if (params
!=
null)
for (1.5.0/docs/api/java/util/Map.html">Map.
Entry<String, Object
> entry : params.
entrySet()) {
transformer.
setParameter(entry.
getKey(), entry.
getValue());
}
// Resulting SAX events (the generated FO) must be piped through to FOP
1.5.0/docs/api/javax/xml/transform/Result.html">Result res =
new 1.5.0/docs/api/javax/xml/transform/sax/SAXResult.html">SAXResult(fop.
getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.
transform(xmlFile, res
);
out.
close();
}
public static void main
(1.5.0/docs/api/java/lang/String.html">String[] args
) {
try {
// URL url = Thread.currentThread().getContextClassLoader().getResource("pt/modiplace/rfid/apis/admin/interfaces/interfaces.fo.xsl");
// Source xsltSource = new StreamSource(new File(url.getPath()));
createPdfFromXml
(new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(new 1.5.0/docs/api/java/io/File.html">File("c:\\test.xml")),
"pt/modiplace/rfid/apis/admin/interfaces/interfaces.fo.xsl",
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(new 1.5.0/docs/api/java/io/File.html">File("c:\\test.pdf")));
}
catch (1.5.0/docs/api/java/io/IOException.html">IOException e
) {
e.
printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch (FOPException e
) {
e.
printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
catch (1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException e
) {
e.
printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}