Skip to content

Commit

Permalink
Adds PDF splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Titou325 committed Mar 14, 2024
1 parent b8abe1d commit dee73b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/client-py/onedoc/onedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from .html_builder import _HtmlBuilder
from .merge import merge as lib_merge
from .split import split as lib_split

from typing import Dict, Union, List, Any, BinaryIO
from typing import Dict, Tuple, Union, List, Any, BinaryIO

DEFAULT_FILE_OPTIONS = {
"cacheControl": "3600",
Expand Down Expand Up @@ -117,3 +118,6 @@ def render(self, document: Dict) -> Dict:

def merge(self, file_a: BinaryIO, file_a_name: str, file_b: BinaryIO, file_b_name: str) -> Pdf:
return lib_merge(file_a, file_a_name, file_b, file_b_name)

def split(self, doc: BinaryIO, page: int) -> Tuple[Pdf, Pdf]:
return lib_split(doc, page)
21 changes: 21 additions & 0 deletions packages/client-py/onedoc/split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pikepdf import Pdf

def split(doc: BinaryIO, page: int) -> Tuple[Pdf, Pdf]:
"""
Split a PDF document at a specific page number.
:param doc: The PDF document to split.
:param page: The page number to split at. Pages before and including this page will be in the first document, and pages after will be in the second document.
:return: A tuple containing the two split PDF documents.
"""
pdf = Pdf.open(doc)
pdf_a = Pdf.new()
pdf_b = Pdf.new()

for i, page in enumerate(pdf.pages):
if i < page:
pdf_a.pages.append(page)
else:
pdf_b.pages.append(page)

return pdf_a, pdf_b

0 comments on commit dee73b9

Please sign in to comment.