user2032522
user2032522

Reputation: 35

Why am I receiving and IOException: PDF header signature not found when creating a PDF?

I trying to create a PDF file with empty pages, but the code throws java.io.IOException: P PDF header signature not found when I'm trying to read the temp file. Why?

Relevant code:

Document testDoc = new Document();
File testFile = File.createTempFile("pdfTemp", ".tmp");
String test = testFile.getName();
PdfWriter testWriter = PdfWriter.getInstance(document, new FileOutputStream(test));
testDoc.open();
for (int x=1; x<=pdfReader.getNumberOfPages(); x++){
    testWriter.setPageEmpty(false);
    testDoc.newPage();
}
testDoc.close();
PdfReader testReader = new PdfReader(test);

Upvotes: 1

Views: 9807

Answers (1)

JoshDM
JoshDM

Reputation: 5052

This may be a coding issue. Your code uses

PdfWriter testWriter = PdfWriter.getInstance(document, new FileOutputStream(test));

but document variable isn't declared anywhere. I suspect you meant to use the following instead:

PdfWriter testWriter = PdfWriter.getInstance(testDoc, new FileOutputStream(test));

Upvotes: 1

Related Questions