Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Stirling-PDF is is a robust, locally hosted web-based PDF manipulation tool using Docker.

Using pdfplumber

import pdfplumber
!wget www.legendu.net/media/wolfram/sum_and_product.pdf
--2023-09-17 15:06:24--  http://www.legendu.net/media/wolfram/sum_and_product.pdf
Resolving www.legendu.net (www.legendu.net)... 185.199.109.153, 185.199.110.153, 185.199.111.153, ...
Connecting to www.legendu.net (www.legendu.net)|185.199.109.153|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 140210 (137K) [application/pdf]
Saving to: ‘sum_and_product.pdf’

sum_and_product.pdf 100%[===================>] 136.92K  --.-KB/s    in 0.03s   

2023-09-17 15:06:24 (4.81 MB/s) - ‘sum_and_product.pdf’ saved [140210/140210]

with pdfplumber.open("sum_and_product.pdf") as pdf:
    img = pdf.pages[0].to_image(resolution=200)
img
<pdfplumber.display.PageImage at 0x7f67bf272d10>
type(img)
pdfplumber.display.PageImage
img.save("0.png")

Using PyMuPDF / fitz

!wajig install python3-fitz
import fitz
pdffile = "source.pdf"
doc = fitz.open(pdffile)
zoom = 4
mat = fitz.Matrix(zoom, zoom)
count = 0
# Count variable is to get the number of pages in the pdf
for p in doc:
    count += 1
for i in range(count):
    val = f"my_image_{i + 1}.png"
    page = doc.load_page(i)
    pix = page.get_pixmap()
    pix.save(val)
doc.close()