Vin*_*raj 5 java html-to-pdf flying-saucer xhtmlrenderer
我刚刚下载了xhtmlrenderer和iText jar文件.我可以使用这些罐子制作pdf文件.
我真正想要的是:如果我https://xhtmlrenderer.dev.java.net/news.html在"inputFile"的位置给出一个有效的URL(比如" "),我需要创建pdf .飞碟和iText有可能吗?
如果是,请指导我实现这一目标.
此外,当我尝试运行以下代码时,我收到错误:流已关闭
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class FirstDoc {
public static void main(String[] args)
throws IOException, DocumentException {
String inputFile = "samples/sql.html";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
Run Code Online (Sandbox Code Playgroud)
是的...这可能不起作用,因为所请求的页面不是 xhtml,但这应该可以解决问题:
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class FirstDoc {
public static void main(String[] args)
throws IOException, DocumentException {
String url= "http://xhtmlrenderer.java.net/news.html";
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
Run Code Online (Sandbox Code Playgroud)
当找不到您请求的文件时,会发生流关闭错误。“samples”文件夹必须存在于您工作区的项目中或您运行应用程序的任何位置
| 归档时间: |
|
| 查看次数: |
10999 次 |
| 最近记录: |