Ryo
Ryo

Reputation: 43

Applying css while converting html to pdf using iText

am trying to convert html to pdf on the fly. I am using iText to achieve this. I succeeded in doing the conversion as such, but am stuck at a situation where I have to apply a external CSS file to convert this HTML to PDF. I searched through the iText mailing list, google for quite sometime now. All I understood is it is not possible to apply external css files.

Can anyone please correct me if I am wrong? Or is there a way to apply external css to convert html to pdf on the fly? Any hints on how to proceed will be much appreciated.

Upvotes: 3

Views: 17936

Answers (2)

Carlos Mtz
Carlos Mtz

Reputation: 151

You can use the external StyleSheet as an internal Stylesheet to test, if everything is ok, use this method

public void generatePDFDesdeDoc()
{
    Document document = new Document();
    PdfWriter writer;
    String lsPath = Environment.getExternalStorageDirectory().getPath();
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                lsPath + getResources().getString(R.string.path_pdf)));
        document.open();
        try {
            XMLWorkerHelper.getInstance().parseXHtml(writer, document
            , new FileInputStream(lsPath + getResources().getString(R.string.path_html))
            , new FileInputStream(lsPath + getResources().getString(R.string.path_css))
            );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (NotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (DocumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    document.close();
}

Upvotes: 0

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5563

You can't directly load the external css file, but you can define the styles as specifed in stylesheets using "StyleSheet" class in iText library. Please refer below URL for example.

http://itextpdf.com/examples/iia.php?id=56

Upvotes: 1

Related Questions