2

I have below iText pdf creation code, basically I need PdfStamper in my later part of code to render html content but it is throwing exception InvalidPdfException while creating PdfStamper:

public static void main(String[] args) throws IOException, DocumentException {

        String TEMP_PDF = "temp.pdf";
        String RESULT = "output1.pdf";
        OutputStream osTemp = null;
        OutputStream osResult = null;
        PdfWriter writer = null;
        PdfReader reader=null;
        PdfStamper stamper=null;
        Document document = new Document(PageSize.LETTER);

        try {
            osTemp = new FileOutputStream(TEMP_PDF);
            osResult = new FileOutputStream(RESULT);
            writer = PdfWriter.getInstance(document, osTemp);
            reader = new PdfReader(TEMP_PDF);
            stamper = new PdfStamper(reader, osResult);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            osTemp.close();
            osResult.close();
            writer.close();
            reader.close();
            stamper.close();
        }
    }

EDIT:

public void createPdf(String file) throws DocumentException, IOException {
        Document document = new Document(PageSize.LETTER);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        int[] coords = new int[] {1, 50, 50, 100, 100} ;
        PdfContentByte canvas = pdfWriter.getDirectContent();
        ColumnText columnText = new ColumnText(canvas);
        String css = "";
        //llx, lly, urx, ury
        columnText.setSimpleColumn(coords[1], coords[2], coords[3], coords[4]);
        ElementList elements = XMLWorkerHelper.parseToElementList("<html><body><b>Bold text</b></body></html>", css);
        for (Element element : elements) {
            columnText.addElement(element);
        }
        columnText.go();
        document.close();
    }
Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41
  • Possible duplicate of [Invalidpdfexception pdf header signature not found](http://stackoverflow.com/questions/12357126/invalidpdfexception-pdf-header-signature-not-found) – Bruno Lowagie Oct 12 '15 at 06:33

1 Answers1

5

You have two problems:

Problem 1:

PdfReader can only read real PDF files. Those are files that start with %PDF-1 and end with %%EOF. In your case, you don't have such a file. You are reading TEMP_PDF which is a file with 0 bytes. In iText, you create a PDF file in 5 steps. You only have step 1 (creating the document) and step 2 (creating the writer) of the creating process. You are missing steps 3 (opening the document), 4 (adding content) and 5 (closing the document).

You only have a complete PDF document after step 5. It is normal that you get an InvalidPdfException as you aren't reading a complete PDF.

Problem 2:

You write: I need PdfStamper in my later part of code to render HTML content.

This is wrong. PdfStamper is a class that can be used to stamp new content (a watermark, form field values, a header, a footer) on an existing PDF document. In no way does PdfStamper convert PDF to HTML or render PDF as HTML.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • thanks for your reply. I have followed 5 steps and then I have used `PdfStamper` it worked. Sorry for the misunderstanding for the problem 2, actually I was mentioning http://stackoverflow.com/q/30651554/1371499 – Nitesh Virani Oct 12 '15 at 08:40
  • OK, one more thing: if you create the PDF in 5 steps. Why do you still need `PdfStamper`? Can't you add the content you want to add with `PdfStamper` in step 4 while creating the document? The answer can be "no": there may be reasons why you need *two passes*. I'm just being curious. – Bruno Lowagie Oct 12 '15 at 08:42
  • I think you are correct, you are just solving my problems :) – Nitesh Virani Oct 12 '15 at 09:16
  • I have updated my question, please have a look at it. It is working fine without creating `PdfStamper` – Nitesh Virani Oct 12 '15 at 09:18
  • That's a good start. However, you need to look at some more `ColumnText` examples. Right now, all content that doesn't fit the page will be lost. You need to check the return value of the `go()` method and create a new page if necessary. It might even be much simpler if you don't use `ColumnText` at all, but if you add the elements to the `document` using `document.add()` instead. – Bruno Lowagie Oct 12 '15 at 09:20
  • it is good to know from author :) `document.add()` is easy option to add element but I want to place it at specific co-ordinates that is why I am using `ColumnText` – Nitesh Virani Oct 12 '15 at 09:44