-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow transferring binary data from JS to Python without serializatio…
…n (CC #67) (#103) * Transfer blobs without serialization (CC #67) * nits * rework print * style improvements, add debug() calls * example/pdfjs: allow URL input It sometimes works, sometimes not. See below for examples: Working: https://cinelerra-gg.org/download/CinelerraGG_Manual.pdf https://pikepdf.readthedocs.io/_/downloads/en/latest/pdf/ Failing: https://www.gemeinde-grindelwald.ch/wp-content/uploads/2020/06/Broschuere_Landwirtschaft_und_Tourismus_Franzoesisch.pdf -> getHexString reports invalid characters. File renders when downloaded, though. Further, the document below produces an out of memory error, but also when rendering locally https://bro.jrtag.ch/Gri/so_guide/de_en/files/downloads/Infoguide_Sommer_23-WEB.pdf * Add test case for surrounding newlines specifically * further polish test cases * make blob debug output less confusing * test: fix wrong timing endpoint * prefer consistent spelling (com vs comm) * nit: use consistent test message * test nits - match test name letter case - add print and 2 more test values - use list comp
- Loading branch information
Showing
6 changed files
with
219 additions
and
54 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# SPDX-FileCopyrightText: 2023 mara004 aka geisserml <[email protected]> | ||
# SPDX-License-Identifier: CC-BY-4.0 OR Apache-2.0 | ||
|
||
# See also https://gist.github.com/mara004/87276da4f8be31c80c38036c6ab667d7 | ||
|
||
# Py-Depends: Pillow, JsPyBridge itself | ||
# Js-Depends: pdfjs-dist, canvas | ||
# Use `python -m pip install` and `python -m javascript --install` | ||
|
||
import argparse | ||
from pathlib import Path | ||
import PIL.Image | ||
import javascript | ||
|
||
# NOTE canvas must be the build pdfjs is linked against, otherwise it'll fail with type error | ||
pdfjs = javascript.require("pdfjs-dist") | ||
libcanvas = javascript.require("canvas") | ||
|
||
|
||
def render_pdf(input, outdir, scale): | ||
|
||
pdf = pdfjs.getDocument(input).promise | ||
n_pages = pdf.numPages | ||
n_digits = len(str(n_pages)) | ||
|
||
for i in range(1, n_pages+1): | ||
|
||
page = pdf.getPage(i) | ||
viewport = page.getViewport({"scale": scale}) | ||
w, h = int(viewport.width), int(viewport.height) | ||
|
||
canvas = libcanvas.createCanvas(w, h) | ||
context = canvas.getContext("2d") | ||
page.render({"canvasContext": context, "viewport": viewport}).promise | ||
|
||
# note that blobValueOf() is much faster than valueOf()["data"] for large byte buffers | ||
js_buffer = canvas.toBuffer("raw") | ||
py_buffer = js_buffer.blobValueOf() | ||
|
||
pil_image = PIL.Image.frombuffer("RGBX", (w, h), py_buffer, "raw", "BGRX", 0, 1) | ||
pil_image.save(outdir / f"out_{i:0{n_digits}d}.jpg") | ||
|
||
pdf.destroy() | ||
|
||
|
||
def main(): | ||
|
||
parser = argparse.ArgumentParser( | ||
description="Render a PDF file with Mozilla pdf.js via JsPyBridge.\n" + | ||
"Known issues: - URL support is buggy; - certain PDFs may hit memory limits.", | ||
) | ||
path_type = lambda p: Path(p).expanduser().resolve() | ||
input_type = lambda p: p if p.startswith("http") else str(path_type(p)) | ||
parser.add_argument( | ||
"input", type=input_type, | ||
help="Input file path or URL.", | ||
) | ||
parser.add_argument("--outdir", "-o", type=path_type) | ||
parser.add_argument("--scale", type=float, default=4) | ||
|
||
args = parser.parse_args() | ||
if not args.outdir.exists(): | ||
args.outdir.mkdir(parents=True, exist_ok=True) | ||
|
||
render_pdf(args.input, args.outdir, scale=args.scale) | ||
|
||
|
||
main() |
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
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
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
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