Last active
April 1, 2019 08:47
-
-
Save killthekitten/2f5acc5799c8166902ce95d43feeb9c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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