Skip to contentSkip to conversation

PDFio: Replaced QPDF by PDFio in libcupsfilters as GSoC project?

Printing-architecture Till Kamppeter 4 participants 9 messages Dec 7, 2023 – Dec 11, 2023

lore
Share & tools
lore
#0 lore raw

Hi,

Michael Sweet has closed the PDFio issue in which I ask for support of PDF forms and annotations, telling that inside PDFio nothing further needs to be done, that everything can be solved outside PDFio, by the calling application.

As we want to get rid of C++ in libcupsfilters I plan to let it use PDFio where it is currently using QPDF.

Mike, as you tell that PDFio is OK for handling PDF files with filled forms/annotations, I want to post a GSoC project idea to switch over libcupsfilters from using QPDF to PDFio. WDYT? Is this possible already? Or are there still any features missing in PDFio?

Till

#1 lore raw

Till,

Quoted 9 lines · Till Kamppeter

Hi,

Michael Sweet has closed the PDFio issue in which I ask for support of PDF forms and annotations, telling that inside PDFio nothing further needs to be done, that everything can be solved outside PDFio, by the calling application.

As we want to get rid of C++ in libcupsfilters I plan to let it use PDFio where it is currently using QPDF.

Mike, as you tell that PDFio is OK for handling PDF files with filled forms/annotations, I want to post a GSoC project idea to switch over libcupsfilters from using QPDF to PDFio. WDYT? Is this possible already? Or are there still any features missing in PDFio?

It is entirely possible. The ipptransform program (part of the libcups project) shows how to do this, but it is really only necessary if you are doing N-up or other combinations of multiple pages from the input document. If all you do is copy pages as-is then pdfioPageCopy will handle copying all of the annotations referenced by the Annots key in the page dictionary.

(ipptransform needs to be updated to copy/merge the Annots values, and I filed issue #71 in the libcups project to track that change...)

________________________
Michael Sweet

#2 lore raw

Hi,

AFAIR the hard thing was exactly this: To transform(move, rotate) the annotations (just keeping them when no transformations are applied was never the problem), as required esp. for n-up.

At the time, the "easiest" way – actually the only way I've seen it implemented in other open source projects – was to first convert the annotations into regular /Content commands (called "flatten" – implemented by Jay in QPDF) that are then simply subject to all the transformations applied there (which is not the case for /Annots, IIRC). AFAIK this also the only way to "clip" annotations to only be drawn inside a given area.

But I would be delighted to see a different implementation.

As a side note, when sufficiently complex transformations are required (e.g. n-up), the current qpdf based pdftopdf implementation converts pages to "Form XObjects", that could be draw just like Images onto new pages. But these XObjects cannot have Annotations themselves, only the Page where they would be drawn to has /Annots.

  Tobias

Quoted 24 lines · Michael Sweet via Printing-architecture

Till,

Quoted 9 lines · Till Kamppeter

Hi,

Michael Sweet has closed the PDFio issue in which I ask for support of PDF forms and annotations, telling that inside PDFio nothing further needs to be done, that everything can be solved outside PDFio, by the calling application.

As we want to get rid of C++ in libcupsfilters I plan to let it use PDFio where it is currently using QPDF.

Mike, as you tell that PDFio is OK for handling PDF files with filled forms/annotations, I want to post a GSoC project idea to switch over libcupsfilters from using QPDF to PDFio. WDYT? Is this possible already? Or are there still any features missing in PDFio?

It is entirely possible.  The ipptransform program (part of the libcups project) shows how to do this, but it is really only necessary if you are doing N-up or other combinations of multiple pages from the input document.  If all you do is copy pages as-is then pdfioPageCopy will handle copying all of the annotations referenced by the Annots key in the page dictionary.

(ipptransform needs to be updated to copy/merge the Annots values, and I filed issue #71 in the libcups project to track that change...)

________________________
Michael Sweet

_______________________________________________
Printing-architecture mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture

#3 lore raw
Quoted 2 lines · Tobias Hoffmann

AFAIR the hard thing was exactly this: To transform(move, rotate) the annotations (just keeping them when no transformations are applied was never the problem), as required esp. for n-up.

Yes, this is what I mean what has to be taken care off, independent of how many input pages are put on one output page.

Quoted 2 lines

At the time, the "easiest" way – actually the only way I've seen it implemented in other open source projects – was to first convert the annotations into regular /Content commands (called "flatten" – implemented by Jay in QPDF) that are then simply subject to all the transformations applied there (which is not the case for /Annots, IIRC). AFAIK this also the only way to "clip" annotations to only be drawn inside a given area.

Yes, for printing, where it does not matter whether the annotations are separately managed annotations or integral part of the graphical content, it simplifies a lot to flatten the annotations before doing any other operation, and so the risk to introduce bugs lowers a lot.

Quoted 2 lines

But I would be delighted to see a different implementation.

On the other side, keeping the annotations as such while doing the conversions makes the filter function more universal, and not only usable for printing.

Quoted 2 lines

As a side note, when sufficiently complex transformations are required (e.g. n-up), the current qpdf based pdftopdf implementation converts pages to "Form XObjects", that could be draw just like Images onto new pages. But these XObjects cannot have Annotations themselves, only the Page where they would be drawn to has /Annots.

And so probably flattening before the operation simplifies the work even more.

Till

#4 lore raw
In reply to Michael Sweet
Quoted 3 lines · Michael Sweet

It is entirely possible.  The ipptransform program (part of the libcups project) shows how to do this, but it is really only necessary if you are doing N-up or other combinations of multiple pages from the input document.  If all you do is copy pages as-is then pdfioPageCopy will handle copying all of the annotations referenced by the Annots key in the page dictionary.

And will pdfioPageCopy() also be enough if I still put a single input page onto the output page but scale and/or crop the page? Will the annotations or filled in content also be appropriately scaled, moved, and cropped?

Quoted 1 line

(ipptransform needs to be updated to copy/merge the Annots values, and I filed issue #71 in the libcups project to track that change...)

So this issue is for solving the above on the level of ipptransform? Would it be then not better to have it in PDFio so that it gets re-used in libcupsfilters?

Till

#5 lore raw
In reply to Till Kamppeter

Hello printing folks,

If you don't mind, may I chime in from the user's perspective? As you know, there are lots of bad PDFs out there, and even if they represent only 0.1% of the total number of all PDFs that people print, one million print jobs turns into 1000 tickets about unprintable files. QPDF is extremely accommodating about it: no matter how bad a PDF is, it will try to recover and proceed. Over the last few years I filed numerous requests about handling some despicable PDFs, and all of them were accepted.

How is PDFio with regards to handling bad PDFs? Is there any testing tool with PDFio compiled in that I can run against my corpus of bad PDFs to see how it would treat them?

Quoted 21 lines · Till Kamppeter

Hi,

Michael Sweet has closed the PDFio issue in which I ask for support of PDF forms and annotations, telling that inside PDFio nothing further needs to be done, that everything can be solved outside PDFio, by the calling application.

As we want to get rid of C++ in libcupsfilters I plan to let it use PDFio where it is currently using QPDF.

Mike, as you tell that PDFio is OK for handling PDF files with filled forms/annotations, I want to post a GSoC project idea to switch over libcupsfilters from using QPDF to PDFio. WDYT? Is this possible already? Or are there still any features missing in PDFio?

Till
_______________________________________________
Printing-architecture mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture

Signature · 1 line
-Alex
#6 lore raw

Alex,

Quoted 7 lines · Alex Korobkin

Hello printing folks,

If you don't mind, may I chime in from the user's perspective? As you know, there are lots of bad PDFs out there, and even if they represent only 0.1% of the total number of all PDFs that people print, one million print jobs turns into 1000 tickets about unprintable files. QPDF is extremely accommodating about it: no matter how bad a PDF is, it will try to recover and proceed. Over the last few years I filed numerous requests about handling some despicable PDFs, and all of them were accepted.

How is PDFio with regards to handling bad PDFs? Is there any testing tool with PDFio compiled in that I can run against my corpus of bad PDFs to see how it would treat them?

The "testpdfio" program specifically does this. With no arguments it runs PDFio's unit tests, but if you pass in a filename you'll get a list of pages/objects:

./testpdfio FILENAME.pdf

./testpdfio --verbose FILENAME.pdf

./testpdfio FILENAME.pdf OBJECT-NUMBER >OBJ.dat

I also have a large corpus I test with (which includes the PDF Association's Stressful PDF Corpus) and the only two open issues I have are:

1. Currently PDFio will not reconstruct a missing/broken xref table, and
2. I have one file that uses two different encryption keys for different parts of the file (this looks to be an encrypted file that was later edited with Acrobat, with the changes written with a separate key...)

I have my concerns about enabling #1 by default, as it could open up security issues (denial-of-service for sure, possibly data corruption).
For #2 I'll come up with a fix but this is the first time I've ever seen a PDF file with multiple encryption keys (the spec doesn't disallow it but also doesn't talk about needing to support it...) so I'm not sure how common they are...

________________________
Michael Sweet

#7 lore raw

Thank you, that's exactly what I needed.
I ran it against my collection of bad PDFs, and 10% of them failed with " Malformed xref table entry '0000000000 65536 f'."
I suppose that's the #1 point from your reply, so perhaps it's not too bad of an idea to reconstruct the xref table when possible?

It also reported "Too many object streams." for one file with 7000 pages, and "Unexpected 'startxref' token seen." on this file created by Till in 2013 to test various page sizes in a single PDF.

Quoted 48 lines · Michael Sweet

Alex,

Quoted 1 line

On Dec 11, 2023, at 11:12 AM, Alex Korobkin <[email protected]>

Quoted 4 lines · wrote:

Hello printing folks,

If you don't mind, may I chime in from the user's perspective? As you

know, there are lots of bad PDFs out there, and even if they represent only 0.1% of the total number of all PDFs that people print, one million print jobs turns into 1000 tickets about unprintable files. QPDF is extremely accommodating about it: no matter how bad a PDF is, it will try to recover and proceed. Over the last few years I filed numerous requests about handling some despicable PDFs, and all of them were accepted.

Quoted 2 lines

How is PDFio with regards to handling bad PDFs? Is there any testing

tool with PDFio compiled in that I can run against my corpus of bad PDFs to see how it would treat them?

The "testpdfio" program specifically does this. With no arguments it runs PDFio's unit tests, but if you pass in a filename you'll get a list of pages/objects:

./testpdfio FILENAME.pdf

./testpdfio --verbose FILENAME.pdf

./testpdfio FILENAME.pdf OBJECT-NUMBER >OBJ.dat

I also have a large corpus I test with (which includes the PDF Association's Stressful PDF Corpus) and the only two open issues I have are:

1. Currently PDFio will not reconstruct a missing/broken xref table, and
2. I have one file that uses two different encryption keys for different parts of the file (this looks to be an encrypted file that was later edited with Acrobat, with the changes written with a separate key...)

I have my concerns about enabling #1 by default, as it could open up security issues (denial-of-service for sure, possibly data corruption). For #2 I'll come up with a fix but this is the first time I've ever seen a PDF file with multiple encryption keys (the spec doesn't disallow it but also doesn't talk about needing to support it...) so I'm not sure how common they are...

________________________
Michael Sweet

Signature · 1 line
-Alex
#8 lore raw

Alex,

Quoted 4 lines · Alex Korobkin

Thank you, that's exactly what I needed.
I ran it against my collection of bad PDFs, and 10% of them failed with " Malformed xref table entry '0000000000 65536 f'."

The allowed range for generation numbers is 0-65535, so this is a bad PDF file. I can certainly allow a wider range of values (will require a small change to the internal representation) or just allow invalid values for object 0 but I'll want to understand the scope of the problem first... Can you file a bug with a file or two that has this problem?

Quoted 1 line

I suppose that's the #1 point from your reply, so perhaps it's not too bad of an idea to reconstruct the xref table when possible?

No, the usual xref table issue is that the offsets are wrong, either from line ending changes or hand editing of the PDF file.

Quoted 1 line

It also reported "Too many object streams." for one file with 7000 pages, and "Unexpected 'startxref' token seen." on this file created by Till in 2013 to test various page sizes in a single PDF.

Really? The current code allows up to 4k object streams per xref table, and most files only have 1 or 2 (Acrobat will make 1 for the first page and 2 for the remaining pages in a linearized file).

Filed this as issue #58:

https://github.com/michaelrsweet/pdfio/issues/58

________________________
Michael Sweet