Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid pdf generation (can only be opened with some PDF renders) #156

Closed
GitMensch opened this issue Mar 6, 2019 · 5 comments · Fixed by #1081
Closed

invalid pdf generation (can only be opened with some PDF renders) #156

GitMensch opened this issue Mar 6, 2019 · 5 comments · Fixed by #1081

Comments

@GitMensch
Copy link
Contributor

GitMensch commented Mar 6, 2019

Firefox shows an empty page, AcrobatReader says:

There was an error processing a page. There was a problem reading this document (109).

Sample code reproducing the issue:

package test.openpdf;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class TestApp 
{
    public static void main( String[] args )
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
        createPDF(bos);
        try {
          Files.write(Paths.get("test.pdf"), bos.toByteArray());
        }
        catch (IOException e) {
          e.printStackTrace();
        }
    }

    public static void createPDF(ByteArrayOutputStream bos) throws DocumentException {
      Document doc = new Document();
      
      PdfWriter pdfWriter = PdfWriter.getInstance(doc, bos);
      doc.open();

      PdfPTable table = new PdfPTable(2);
      table.setWidthPercentage(100f);
      table.setHorizontalAlignment(0);

      table.addCell("row 1");
      table.addCell("some data");
      table.addCell("row 1");
      table.addCell("other data");
      
      doc.add(table);
      
      pdfWriter.close();  // error here...
      doc.close();  // ...in combination with this
    }
}

The main question: Should an error be raised somewhere as OpenPDF was used "wrong" = please handle it as Feature Request to do so (I'd like to know if someone spots what is missing); or is OpenPDF just generating a bad PDF and needs a fix = please handle it as bug report (I'd like to know if someone has a work around) - is this related to #135 ?

Resulting test.pdf
Working pdf, created with Chrome's print as pdf (recreating from rendered version, always as PDF 1.7)

@GitMensch
Copy link
Contributor Author

After some additional checks: the hidden problem is that pdfWriter.close(); is breaking everything. I've gave this a short try but did not found a way to raise a RuntimeException there but assume this would be the correct thing to do.

Workaround: don't use pdfWriter.close();.

@GitMensch
Copy link
Contributor Author

Side question: Wouldn't it be reasonable to provide the createPDF code from https://github.com/LibrePDF/OpenPDF/blob/master/openpdf/src/test/java/com/lowagie/text/pdf/PdfTestBase.java as a static function in Document or PdfWriter (maybe with an optional PageSize parameter)?

@mkl-public
Copy link
Contributor

Workaround: don't use pdfWriter.close();.

You shall never use pdfWriter.close() anyways.

The design here is that pdfWriter is created as an (indirect) multiple event listener of doc, in particular the close call is forwarded from doc to pdfWriter after some finishing pieces have been sent to pdfWriter. Thus, if you close pdfWriter before doc is closed, your result PDF is unfinished; and during a successful doc.close() the pdfWriter is closed implicitly.

@GitMensch
Copy link
Contributor Author

I see. Ideally the visibility would be changed to prevent errors like these during coding - or - if this isn't possible/exceptable a RuntimeCheck thrown if the caller wasn't doc.

What do you think?

@mkl-public
Copy link
Contributor

Ideally the visibility would be changed to prevent errors like these during coding

PdfWriter.close() overrides a parent class method and eventually implements an interface method. Thus, visibility changes hardly will be possible.

if this isn't possible/exceptable a RuntimeCheck thrown if the caller wasn't doc.

Strictly speaking the forwarding is indirect, between Document and PdfWriter there is a PdfDocument. Furthermore, many people close the PdfWriter after closing the Document. This is not bad (duplicate closes are ignored), so this should remain allowed.

With these border conditions in mind, such a runtime check could be implemented.

@asturio asturio self-assigned this Feb 23, 2024
@asturio asturio linked a pull request Feb 23, 2024 that will close this issue
1 task
asturio added a commit that referenced this issue Feb 26, 2024
* #156: Throw Exception if PdfWriter.close() is called directly

- Throw an IllegalStateException if PdfWriter.close() is called before
  Document.close() is called.
- Delete old *.lng files in src/main/java
- Update *.lng files in src/main/resources
- BoyScout rule in PdfWriter

* #156: Throw Exception if PdfWriter.close() is called directly

- Codacy Issues
asturio added a commit that referenced this issue Feb 26, 2024
* #156: Throw Exception if PdfWriter.close() is called directly

- Throw an IllegalStateException if PdfWriter.close() is called before
  Document.close() is called.
- Delete old *.lng files in src/main/java
- Update *.lng files in src/main/resources
- BoyScout rule in PdfWriter

* #156: Throw Exception if PdfWriter.close() is called directly

- Codacy Issues

(cherry picked from commit 6c675f7)
asturio added a commit that referenced this issue Feb 26, 2024
* #156: Throw Exception if PdfWriter.close() is called directly

- Throw an IllegalStateException if PdfWriter.close() is called before
  Document.close() is called.
- Delete old *.lng files in src/main/java
- Update *.lng files in src/main/resources
- BoyScout rule in PdfWriter

* #156: Throw Exception if PdfWriter.close() is called directly

- Codacy Issues

(cherry picked from commit 6c675f7)
@asturio asturio modified the milestones: 1.3.42, 1.4.2, 2.0.1 Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants