gws.lib.pdf

PDF utilities.

Functions to combine PDF files and convert them to images, used mainly by the printer. Combining is done with pypdf, conversion to images with Ghostscript (gs).

  • overlay merges the pages of one PDF on top of the pages of another,

  • concat joins several PDFs into one,

  • page_count returns the number of pages,

  • to_image_path renders a page as PNG or JPEG.

Example:

import gws.lib.pdf

gws.lib.pdf.overlay('/tmp/map.pdf', '/tmp/frame.pdf', '/tmp/page.pdf')
gws.lib.pdf.concat(['/tmp/page1.pdf', '/tmp/page2.pdf'], '/tmp/all.pdf')
gws.lib.pdf.to_image_path('/tmp/all.pdf', '/tmp/preview.png', (400, 300))

Source code: gws.lib.pdf

Package Contents

gws.lib.pdf.concat(paths: list[str], out_path: str) → str

Concatenate multiple PDFs into one.

If only one path is given, nothing is written and that path is returned.

Parameters:
  • paths – Paths to the PDFs.

  • out_path – Path to the output PDF.

Returns:

Path to the concatenated PDF.

gws.lib.pdf.overlay(a_path: str, b_path: str, out_path: str) → str

Overlay two PDFs page by page.

Each page of b is placed on top of the page with the same number in a. The output has as many pages as a; pages of a without a counterpart in b are copied unchanged.

Parameters:
  • a_path – Path to the bottom PDF.

  • b_path – Path to the top PDF.

  • out_path – Path to the output PDF.

Returns:

Path to the output PDF.

gws.lib.pdf.page_count(path: str) → int

Return the number of pages in a PDF.

Parameters:

path – Path to the PDF.

Returns:

The number of pages.

gws.lib.pdf.to_image_path(in_path: str, out_path: str, size: gws.Size, mime_type: str = gws.lib.mime.PNG, page: int = 1) → str

Render a PDF page as an image, using Ghostscript.

The page is scaled to fit the given size.

Parameters:
  • in_path – Path to the input PDF.

  • out_path – Path to the output image.

  • size – Size of the output image in points.

  • mime_type – Mime type of the output image, either PNG or JPEG.

  • page – Page number to convert, starting with 1.

Returns:

Path to the output image.

Raises:
  • ValueError – If the mime type is not supported.

  • gws.lib.osx.Error – If Ghostscript fails.