Skip to content

Instantly share code, notes, and snippets.

@killthekitten
Last active April 1, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killthekitten/2f5acc5799c8166902ce95d43feeb9c2 to your computer and use it in GitHub Desktop.
Save killthekitten/2f5acc5799c8166902ce95d43feeb9c2 to your computer and use it in GitHub Desktop.
import fitz
import io
import requests
PDF_URL = "https://github.com/pymupdf/PyMuPDF/raw/master/examples/colordbHSV.pdf"
PNG_URL = "https://raw.githubusercontent.com/pymupdf/PyMuPDF/master/examples/hand.png"
def handle(_):
"""
The snippet serves two purposes:
1. Illustrate a segfault in PyMuPDF (https://github.com/pymupdf/PyMuPDF/issues/274)
2. Provide a reproducible example of GCP Cloud Functions issue when
a segmentation fault occurs (https://issuetracker.google.com/issues/125425924)
"""
pdf = requests.get(PDF_URL).content
png = io.BytesIO(requests.get(PNG_URL).content)
rect = fitz.Rect(0, 0, 100, 100)
document = fitz.open(stream=pdf, filetype="pdf")
for page in document:
# fitz expects the `stream` arg to be `bytes`, while we pass
# `BytesIO`, which results in a segfault.
page.insertImage(rect, stream=png)
return document.write()
if __name__ == "__main__":
handle(None)
Flask==1.0.2
PyMuPDF==1.14.12
requests==2.21.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment