3

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.