Search code examples
pythonpdffontscolorsdpi

PDF 'advanced' information extraction


I'm trying to write what more or less accounts for a PDF soft proof.

There are a few infos that I would like to extract, but have no clue how to.

What I need to extract:

Bleed:                    I got this somewhat working with pyPdf, given
                          that the document uses 72 dpi, which sadly isn't
                          always the case. I need to be able to calculate
                          the bleed in millimeters.

Print resolution (dpi):   If I read the PDF spec[1] correctly this ought to
                          always be 72 dpi, unless a page has UserUnit set,
                          which was only introduced in PDF-1.6, but shouldn't
                          print documents always be at least 300 dpi? I'm
                          afraid that I misunderstood something…

                          I'd also need the print resolution for images, if
                          they can differ from the default page resolution,
                          that is.

Text color:               I don't have the slightest clue on how to extract
                          this, the string 'text colour' only shows up once
                          in the whole spec, without any explanation how it
                          is set.

Image colormodel:         If I understand it correctly I can read this out
                          in pyPdf with page['/Group']['/CS'] which can be:
                             - /DeviceRGB
                             - /DeviceCMY
                             - /DeviceCMYK
                             - /DeviceGray
                             - /DeviceRGBK
                             - /DeviceN

Font 'embeddedness':      I read in another post on stackoverflow that I
                          can just iterate over the font resources and if a
                          resource has a '/FontFile'-key that means that
                          the font is embedded. Is this correct?

If other libs than pyPdf are better able to extract this info (or a combination of them) they are more than welcome. So far I fumbled around with pyPdf, pdfrw and pdfminer. All of which don't exactly have the most extensive documentation.

[1] http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf


Solution

  • If I read the PDF spec1 correctly this ought to always be 72 dpi, unless a page has UserUnit set, which was only introduced in PDF-1.6, but shouldn't print documents always be at least 300 dpi? I'm afraid that I misunderstood something…

    You do misunderstand something. The default user space unit which defaults to 1/72 inch but can be changed on a per-page base since PDF-1.6, is not defining a print resolution, it merely defines what length a unit in coordinates given by the user by default (i.e. unless any size-changing transformation is active) corresponds to.

    For printing all data are converted into a device dependent space whose resolution has nothing to do with the user space coordinates. Printing resolutions depend on the printing device and their drivers; they may be limitted due to security settings allowing low quality printing only.

    I'd also need the print resolution for images, if they can differ from the default page resolution, that is.

    Images (well, bitmap images, in PDF there are also vector graphics) come each with their individual resolution and then may be transformed (e.g. enlarged) before being rendered. For an "image printing resolution" you'd, therefore, have to inspect each and every bitmap image and each and every page content in which it is inserted. And if the image is rotated, skewed and asymmetrically stretched, I wonder what number you will use as resolution... ;)

    Text color: I don't have the slightest clue on how to extract this, the string 'text colour' only shows up once in the whole spec, without any explanation how it is set.

    Have a look at section 9.2.3 in the spec:

    The colour used for painting glyphs shall be the current colour in the graphics state: either the nonstroking colour or the stroking colour (or both), depending on the text rendering mode (see 9.3.6, "Text Rendering Mode"). The default colour shall be black (in DeviceGray), but other colours may be obtained by executing an appropriate colour-setting operator or operators (see 8.6.8, "Colour Operators") before painting the glyphs.

    There you find a number of pointers to interesting sections. Be aware, though, text is not simply coloured; it may also be rendered as a clip path applied to any background.

    I read in another post on stackoverflow that I can just iterate over the font resources and if a resource has a '/FontFile'-key that means that the font is embedded. Is this correct?

    I would advice a more precise analysis. There are other relevant keys, too, e.g. '/FontFile2' and '/FontFile3', and the correct one must be used.

    Don't underestimate your tasks... you should start to define what the properties you search shall mean in a mixed environment of rotated, stretched and skewed glyphs, vector graphics and bitmap images like PDF.