public class PdfUtil
{
private String wkhtml2pdf_path = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
public InputStream html2pdf(String htmlPath, String pdfPath) {
FileInputStream fi = null;
try
{
String command = getCommand(htmlPath, pdfPath);
File pdfFile = new File(pdfPath);
if (!pdfFile.exists()) {
pdfFile.createNewFile();
}
Process pr = Runtime.getRuntime().exec(command);
StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
pr.waitFor();
fi = new FileInputStream(pdfPath);
} catch (Exception e) {
e.printStackTrace();
}
return fi;
}
private String getCommand(String htmlPath, String pdfPath)
{
return this.wkhtml2pdf_path + " " + htmlPath + " " + pdfPath;
}
public static boolean writeWordFile(String content, String wordPath)
{
boolean w = false;
try
{
File wordfile = new File(wordPath);
if (!wordfile.exists()) {
wordfile.createNewFile();
}
byte[] b = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(wordPath);
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
return w;
}
}
转载请注明原文地址: https://ju.6miu.com/read-1304600.html