android从html文本创建pdf文件

nAk*_*dov 5 html pdf android itext

这是我的代码,用于将 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;
}
Run Code Online (Sandbox Code Playgroud)

}

它适用于以下 htmlText

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;
}
Run Code Online (Sandbox Code Playgroud)

以下 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>
Run Code Online (Sandbox Code Playgroud)

请帮助我为什么它的工作方式不同,并且它给出了第二个 htmlText 的错误document has no pages,并且在此之后输出流为空tidy.parse(new ByteArrayInputStream(htmlText.getBytes("ISO-8859-1")), output);。提前致谢!

nAk*_*dov 2

我最近解决了这个问题。问题出Navruzbek Akhmedov <akhmedovnavruzbek@gmail.com>在 html 文本中。iText lib 在我看来<akhmedovnavruzbek@gmail.com>就像 HTML 标签。它实际上在 html 标签列表中没有,然后给出错误。就这样!:))))))))))))))))))