Skip to content

Commit

Permalink
ENH: Add example with attachment (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Nov 14, 2023
1 parent d87c33e commit 964fb47
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 18 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/ci.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#
# This file is autogenerated by pip-compile with python 3.11
# To update, run:
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile .github/workflows/ci.in
#
pydantic==1.10.4
# via -r .github/workflows/ci.in
pypdf==3.3.0
annotated-types==0.6.0
# via pydantic
pydantic==2.5.0
# via -r .github/workflows/ci.in
typing-extensions==4.4.0
pydantic-core==2.14.1
# via pydantic
pypdf==3.17.0
# via -r .github/workflows/ci.in
typing-extensions==4.8.0
# via
# pydantic
# pydantic-core
17 changes: 11 additions & 6 deletions .github/workflows/json_consistency.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import datetime
import json
import logging
import sys
from pathlib import Path

from pydantic import BaseModel, NonNegativeInt

from pypdf import PdfReader

logger = logging.getLogger()

logger.level = logging.ERROR


class AnnotationCount(BaseModel):
Highlight: int | None
Ink: int | None
Link: int | None
Text: int | None
Widget: int | None
Highlight: int | None = None
Ink: int | None = None
Link: int | None = None
Text: int | None = None
Widget: int | None = None

def items(self) -> list[tuple[str, int]]:
return [
Expand Down Expand Up @@ -47,7 +52,7 @@ def main() -> None:
"""Check the consistency of the JSON metadata file."""
with open("files.json") as f:
data = json.load(f)
main_pdf = MainPdfFile.parse_obj(data)
main_pdf = MainPdfFile.model_validate(data)
registered_pdfs = []

seen_failure = False
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pre-commit run --all-files
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.5.0
hooks:
- id: check-ast
- id: check-case-conflict
Expand All @@ -20,21 +20,21 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py39-plus]
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==22.1.0]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.280'
rev: 'v0.1.5'
hooks:
- id: ruff
args: ['--fix']
10 changes: 10 additions & 0 deletions 025-attachment/add_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pypdf import PdfReader, PdfWriter

reader = PdfReader("../001-trivial/minimal-document.pdf")
writer = PdfWriter()
writer.append_pages_from_reader(reader)

with open("image.png", "rb") as file:
writer.add_attachment("image.png", file.read())
with open("with-attachment.pdf", "wb") as file:
writer.write(file)
Binary file added 025-attachment/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 025-attachment/with-attachment.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
maint:
pip-compile .github/workflows/ci.in --upgrade
12 changes: 11 additions & 1 deletion files.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@
"Highlight": 1,
"Ink": 1
}
},
{
"path": "025-attachment/with-attachment.pdf",
"producer": "pypdf",
"creation_date": null,
"encrypted": false,
"pages": 1,
"images": 1,
"forms": 0,
"annotations": {}
}
]
}
}

0 comments on commit 964fb47

Please sign in to comment.