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。我使用下面的代码,但是当我点击生成器按钮时,应用程序突然关闭,并且SD卡中没有pdf文件(我的xml和xsl文件在布局目录中)。