在JApplet中使用iText将html文件转换为pdf [英] Converting html file to pdf using iText in JApplet
问题描述
我正在使用iText进行项目。我的程序应该从浏览器内部运行,我需要它将html文件转换为pdf文件。当我从NetBeans运行程序时,一切正常。我签署我的jar并在浏览器中运行Applet然后我收到此错误:
I am using iText for a project. My program is supposed to run from inside a browser and I need it to convert an html file to a pdf file. When I run the program from NetBeans everything works fine. I sign my jar and run the Applet in a browser and then I get this error:
Errorjava.security.AccessControlException:access denied(java .lang.RuntimePermissiongetenv.windir)
为了这篇文章的目的,我做了一个简单的JApplet代码,它有相同的问题:
For the purpose of this post I have made a simple JApplet code which has the same problem:
public class RunApplet extends JApplet {
@Override
public void init() {
this.add(new JLabel("This is a labe"));
File f = new File("C:/ReportGen/data.html");
File pdf = new File("C:/ReportGen/data.pdf");
try {
pdf.createNewFile();
Document pdfDocument = new Document();
PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdf));
pdfDocument.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
FontFactoryImp imp = new FontFactoryImp();
imp.getFont("Arial");
FontFactory.setFontImp(imp);
worker.parseXHtml(writer, pdfDocument, new FileInputStream(f));
pdfDocument.close();
writer.close();
this.add(new JLabel(f.getAbsolutePath()));
} catch (Exception ex) {
this.add(new JTextField("Error"+ex));
}
}
}
创建了html文件,很好,但是当我创建pdf文件时,我得到异常并且实际创建了pdf文件,但是它已损坏而我无法打开它。提前感谢您的时间。
The html file is created and is fine, but when I create the pdf file I get the exception and the pdf file is actually created, but is corrupt and I am unable to open it. Thanks in advance for your time.
推荐答案
首先,我在您的问题中看到此错误:
First, I see this error in your question:
Errorjava.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getenv.windir")
您需要签署您的applet才能访问您的文件系统。请参阅此链接和这个。
You need signed your applet for access to your filesystem. See this link and this too.
其次,我尝试过以下代码:
Second, I have tried following code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class main {
public static void main(String[] args) {
File f = new File("C:/tmp/data.htm");
File pdf = new File("C:/tmp/data.pdf");
Document pdfDocument = null;
PdfWriter pdfWriter = null;
try {
pdfDocument = new Document();
pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdf));
pdfDocument.open();
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, pdfDocument,
new FileInputStream(f));
pdfDocument.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
如果是文件data.htm(原始数据)是一个htmlx,工作正常。但是如果data.htm不是xml,我会收到这个错误:
If file data.htm (original data) is an htmlx, work fine. But if data.htm not is an xml, i get this error:
com.itextpdf.tool.xml.exceptions.RuntimeWorkerException: Invalid nested tag head found, expected closing tag meta.
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:134)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:220)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:185)
at main.main(main.java:44)
您可以尝试使用您的数据并使用此示例?区别在于我的示例不是applet,是java独立版。
Can you try with your data and with this example? The difference is that my example isn't an applet, is an java standalone.
问候
这篇关于在JApplet中使用iText将html文件转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!