安卓如何从HTML文本创建PDF文件

5
这是我用来将HTML转换为PDF的代码:
public boolean create (String htmlText, String absoluteFilePath) {
try {
    Document document = new Document(PageSize.LETTER);
    PdfWriter pdfWriter = PdfWriter.getInstance
            (document, new FileOutputStream(absoluteFilePath));
    document.open();

    // Fixing xhtml tag
    Tidy tidy = new Tidy(); // obtain a new Tidy instance
    tidy.setXHTML(true); // set desired config options using tidy setters
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    tidy.setCharEncoding(Configuration.UTF8);
    tidy.parse(new ByteArrayInputStream(htmlText.getBytes(), output);
    String preparedText = output.toString("UTF-8");

    Log.i("CHECKING", "JTidy Out: " + preparedText);

    InputStream inputStream = new ByteArrayInputStream(preparedText.getBytes());
    XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document,
            inputStream, null, Charset.forName("UTF-8"), new MyFont());

    document.close();
    return true;
} catch (Exception e) {
    File file = new File(absoluteFilePath);
    if(file.exists()) {
        boolean isDeleted = file.delete();
        Log.i("CHECKING", "PDF isDeleted: " + isDeleted);
    }
    LOGGER.error("Exception: " + e.getMessage());
    e.printStackTrace();
    return false;
}

它适用于以下htmlText

   <p dir="ltr"><br>
wwwww<br>
--- <br>
Sent bys.</p>


<p>Original message:</p>
<blockquote>
<strong>From: </strong>
nakhmedov@s.com
<br/>
<strong>Sent: </strong>
Dec 1, 2014 5:10:19 PM
<br/>
<strong>
To: 
</strong>
ssss
<br/>
<strong>Subject: </strong>
test
<br/>
<br/>
<p dir="ltr">
<br>
123<br>
--- <br>
ssssssss.</p>
</blockquote>

以下 htmlText 不起作用:

<p dir="ltr"><br>
123<br>
--- <br>
Sent by ss.</p>


<p>Original message:</p>
<blockquote>
<strong>From: </strong>
Navruzbek Akhmedov <akhmedovnavruzbek@gmail.com>
<br/>
<strong>Sent: </strong>
Dec 1, 2014 5:14:36 PM
<br/>
<strong>
To: 
</strong>
Navruzbek Akhmedov <nakhmedov@sss.com>
<br/>
<strong>Subject: </strong>
test
<br/>
<br/>
<div dir="ltr">12345</div>
</blockquote>

请帮我解释为什么它对第二个htmlText的处理与之前不同,并在执行tidy.parse(new ByteArrayInputStream(htmlText.getBytes("ISO-8859-1")), output);后输出流为空,报错信息为document has no pages。感谢您的帮助!


我有什么遗漏吗?也许HTML文本无效,因此无法正常工作! - nAkhmedov
它运行得非常好:))))))))) - nAkhmedov
你正在使用iText来转换PDF吗? - Hunt
@Hunt 是的 :) compile 'com.itextpdf:itextpdf:5.5.3' compile 'com.itextpdf.tool:xmlworker:5.5.3' compile 'jtidy:jtidy:4aug2000r7-dev' - nAkhmedov
1个回答

2

最近我解决了这个问题。问题在于HTML文本中的Navruzbek Akhmedov <akhmedovnavruzbek@gmail.com>。iText库似乎将<akhmedovnavruzbek@gmail.com>视为HTML标记。因为它实际上不在HTML标记列表中,所以会出现错误。就是这样!:)))))))))))))))))))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接