-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pageobjects: add initial image support model (#151)
- Loading branch information
Showing
20 changed files
with
358 additions
and
80 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
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
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,57 @@ | ||
# SPDX-FileCopyrightText: 2022 geisserml <[email protected]> | ||
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
import os.path | ||
from pypdfium2 import _namespace as pdfium | ||
|
||
|
||
def attach_parser(subparsers): | ||
parser = subparsers.add_parser( | ||
"jpegtopdf", | ||
help = "Convert JPEG images to PDF", | ||
) | ||
parser.add_argument( | ||
"images", | ||
nargs = "+", | ||
help = "Input JPEG images", | ||
type = os.path.abspath, | ||
) | ||
parser.add_argument( | ||
"--output", "-o", | ||
required = True, | ||
help = "Target path for the new PDF" | ||
) | ||
parser.add_argument( | ||
"--inline", | ||
action = "store_true", | ||
help = "Whether to use FPDFImageObj_LoadJpegFileInline() rather than FPDFImageObj_LoadJpegFile()." | ||
) | ||
|
||
|
||
def main(args): | ||
|
||
# Very rudimentary JPEG to PDF conversion, mostly for testing | ||
# The implementation could certainly be more sophisticated (e. g. configurable DPI, margins, crop, positioning, ...) | ||
|
||
pdf = pdfium.PdfDocument.new() | ||
|
||
for file in args.images: | ||
|
||
# Simple check if the input files are actually JPEGs | ||
# A better implementation could use mimetypes or python-magic instead | ||
assert any(file.lower().endswith(ext) for ext in (".jpg", ".jpeg")) | ||
|
||
image = pdfium.PdfImageObject.new(pdf) | ||
|
||
buffer = open(file, "rb") | ||
width, height = image.load_jpeg(buffer, inline=args.inline, autoclose=True) | ||
|
||
page = pdf.new_page(width, height) | ||
page.insert_object(image) | ||
page.generate_content() | ||
|
||
if os.path.exists(args.output): | ||
raise FileExistsError("Refusing to overwrite '%s'" % args.output) | ||
|
||
with open(args.output, "wb") as buffer: | ||
pdf.save(buffer) |
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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# SPDX-FileCopyrightText: 2022 geisserml <[email protected]> | ||
# SPDX-FileCopyrightText: 2022 Anurag Bansal <[email protected]> | ||
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
import math | ||
|
@@ -263,6 +262,9 @@ def insert_text( | |
PDF font data. | ||
""" | ||
|
||
# User-contributed code | ||
# SPDX-FileCopyrightText: 2022 Anurag Bansal <[email protected]> | ||
|
||
hb_buffer = harfbuzz.Buffer() | ||
hb_buffer.add_str(text) | ||
hb_buffer.guess_segment_properties() | ||
|
@@ -316,15 +318,16 @@ def get_objects(self, max_depth=2, form=None, level=0): | |
if raw_obj is None: | ||
raise PdfiumError("Failed to get page object.") | ||
|
||
helper_obj = PdfPageObject( | ||
type = pdfium.FPDFPageObj_GetType(raw_obj) | ||
yield PdfPageObject( | ||
raw = raw_obj, | ||
type = type, | ||
page = self, | ||
pdf = self.pdf, | ||
level = level, | ||
) | ||
yield helper_obj | ||
|
||
if level < max_depth-1 and helper_obj.type == pdfium.FPDF_PAGEOBJ_FORM: | ||
if level < max_depth-1 and type == pdfium.FPDF_PAGEOBJ_FORM: | ||
yield from self.get_objects( | ||
max_depth = max_depth, | ||
form = raw_obj, | ||
|
@@ -338,10 +341,10 @@ def render_to(self, converter, **renderer_kws): | |
Parameters: | ||
converter (BitmapConvBase | typing.Callable): | ||
A translator to convert the output of :meth:`.render_base`. | ||
See :class:`.BitmapConv` for a set of built-in converters. | ||
A translator to convert the output of :meth:`.render_base`. See :class:`.BitmapConv` for a set of built-in converters. | ||
renderer_kws (dict): | ||
Keyword arguments to the renderer. | ||
Returns: | ||
typing.Any: Converter-specific result. | ||
|
Oops, something went wrong.