Android create pdf file from html text

Here is my code to convert html to 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;
}

      

}

It works for this following 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 codeHide result


And it doesn't work on the following 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>
      

Run codeHide result


Please help me why it works differently and it gives error for secon htmlText document has no pages

and after that the output stream is empty after tidy.parse(new ByteArrayInputStream(htmlText.getBytes("ISO-8859-1")), output);

. Thanks in advance!

+3


source to share


2 answers


I solved this problem recently. The problem was in the Navruzbek Akhmedov <akhmedovnavruzbek@gmail.com>

html text. The iText lib seems to see <akhmedovnavruzbek@gmail.com>

as an HTML tag. It doesn't actually have html tags listed and then gives an error. All this!:)))))))))))))))))))



+1


source


I am getting this below error when starting the project

Error: Program Type Already Present: com.itextpdf.awt.geom.AffineTransform

Using items below gradle



implementation 'com.itextpdf: itextg: 5.5.10'

implementation of 'com.itextpdf.tool: xmlworker: 5.5.3'

implementation of 'jtidy: jtidy: 4aug2000r7-dev'

0


source







All Articles