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

Check for unmatched doc.restore() calls? #271

Open
realtaraharris opened this issue Aug 14, 2014 · 3 comments
Open

Check for unmatched doc.restore() calls? #271

realtaraharris opened this issue Aug 14, 2014 · 3 comments

Comments

@realtaraharris
Copy link

It's easy to produce a corrupt PDF by inserting a stray unpaired doc.restore() into code that uses pdfkit.

Is checking for stuff like this something pdfkit should try to do?

These errors are hard to detect because the resulting file renders without error in the better PDF viewers out there, including Preview and pdf.js. Unfortunately, Acrobat stops rendering after encountering errors such as these and makes no attempt to explain what's wrong.

@devongovett
Copy link
Member

Hmm, yes. Right now most commands get rendered directly into the document as soon as you call them, but I'm thinking of moving to a model where calls are batched and can be more easily checked for user error. Thanks for reporting. I didn't know Acrobat complained about this.

@sly7-7
Copy link

sly7-7 commented Aug 27, 2021

I just encounter this error as well, seems like acrobat still fails on it. (browsers are ok, OS X preview too, as well as affinity publisher).
Fortunately, it's a quite small document, so it was quite easy to find out the restore() call without previous save().

@backspace
Copy link
Contributor

backspace commented Nov 18, 2023

This happened to me recently, I found that:

  • Illustrator would not open certain pages from a PDF
  • Ghostscript said encountered more Q than q (or the opposite)
  • pdftocairo said Restoring state when no valid states to pop

I tried printing at a chain printshop and it choked on the pages:

IMG_1021

The code to generate the PDF is quite involved and superficially I see an equal number of save() and restore() calls, though there’s a lot of nesting so something’s surely wrong somewhere. It would be nice to have something to ease tracking this down.

Regardless, thanks for all the work on this, it’s much nicer to work with than the hideous hacks I was using in the past 😬

ETA: I added this hack to track the save/restore stack:

    let oldSave = doc.save;
    let oldRestore = doc.restore;

    let stack = 0;

    doc.save = function () {
      stack++;
      console.log(`save called, stack: ${stack}`);
      oldSave.call(doc);
    };

    doc.restore = function () {
      stack--;
      console.log(`restore called, stack: ${stack}`);
      oldRestore.call(doc);
    };

The last line logged is this: restore called, stack: 0

So it does seem like they balance out! I’m sure I did something wrong somewhere but I’m not sure how to track it down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants