我剛剛下載了xhtmlrenderer和iText jar文件。我可以使用這些罐子製作pdf文件。如何使用iText&飛碟將java頁面的url轉換爲java中的pdf?
我確切需要的是: 如果我給一個有效的URL(例如「https://xhtmlrenderer.dev.java.net/news.html
」)代替「inputFile」,我需要創建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();
}
}
您的代碼包含'com.lowagie'。這是iText的一個古老版本。請使用'com.itextpdf' iText。 – 2016-04-10 06:55:12