public void onClick(View v)
{switch(v.getId())
{ case R.id.button1:
try {
// Setup directories
File baseDir = new File("res/layout");
File outDir = new File(baseDir, "/sdcard");
outDir.mkdirs();
// Setup input and output files
File xmlfile = new File(baseDir, "activity_main.xml");
File xsltfile = new File(baseDir, "test.xsl");
File pdffile = new File(outDir, "ResultXML2PDF.pdf");
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "2.0");
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
}
标题
我想将我的 xml 文件(包括我的视图)转换为 pdf .. 我的 android 应用程序必须生成这个 pdf 。我使用下面的代码,但是当我单击生成器按钮时,应用程序突然关闭,并且 sdcard 中没有文件 pdf(我的 xml 和 xsl 文件在布局目录中)。