Skip to content

Commit

Permalink
Merge branch 'main' into reader-minor-sty
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Sep 14, 2024
2 parents ac2983b + 8ebd311 commit 1f0861f
Show file tree
Hide file tree
Showing 24 changed files with 577 additions and 1,673 deletions.
24 changes: 24 additions & 0 deletions docs/user/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ writer.add_metadata(
}
)

# Clear all data but keep the entry in PDF
writer.metadata = {}

# Replace all entries with new set of entries
writer.metadata = {
"/Author": "Martin",
"/Producer": "Libre Writer",
}

# Save the new PDF to a file
with open("meta-pdf.pdf", "wb") as f:
writer.write(f)
```

## Removing metadata entry

```python
from pypdf import PdfWriter

writer = PdfWriter("example.pdf")

# Remove Metadata (/Info entry)
writer.metadata = None

# Save the new PDF to a file
with open("meta-pdf.pdf", "wb") as f:
writer.write(f)
Expand Down
7 changes: 4 additions & 3 deletions pypdf/_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from .generic import (
DecodedStreamObject,
DictionaryObject,
NullObject,
StreamObject,
is_null_or_none,
)


Expand Down Expand Up @@ -468,7 +468,7 @@ def compute_space_width(
cpt += 1
sp_width = m / max(1, cpt) / 2

if sp_width is None or isinstance(sp_width, NullObject):
if is_null_or_none(sp_width):
sp_width = 0.0
return sp_width

Expand All @@ -482,8 +482,9 @@ def type1_alternative(
if "/FontDescriptor" not in ft:
return map_dict, space_code, int_entry
ft_desc = cast(DictionaryObject, ft["/FontDescriptor"]).get("/FontFile")
if ft_desc is None:
if is_null_or_none(ft_desc):
return map_dict, space_code, int_entry
assert ft_desc is not None, "mypy"
txt = ft_desc.get_object().get_data()
txt = txt.split(b"eexec\n")[0] # only clear part
txt = txt.split(b"/Encoding")[1] # to get the encoding part
Expand Down
5 changes: 3 additions & 2 deletions pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
TreeObject,
ViewerPreferences,
create_string_object,
is_null_or_none,
)
from .types import OutlineType, PagemodeType
from .xmp import XmpInformation
Expand Down Expand Up @@ -761,7 +762,7 @@ def _get_inherited(obj: DictionaryObject, key: str) -> Any:
field = cast(DictionaryObject, field.indirect_reference.get_object()) # type: ignore
except Exception as exc:
raise ValueError("field type is invalid") from exc
if _get_inherited(field, "/FT") is None:
if is_null_or_none(_get_inherited(field, "/FT")):
raise ValueError("field is not valid")
ret = []
if field.get("/Subtype", "") == "/Widget":
Expand Down Expand Up @@ -852,7 +853,7 @@ def _get_outline(
return outline

# §12.3.3 Document outline, entries in the outline dictionary
if lines is not None and "/First" in lines:
if not is_null_or_none(lines) and "/First" in lines:
node = cast(DictionaryObject, lines["/First"])
self._namedDests = self._get_named_destinations()

Expand Down
Loading

0 comments on commit 1f0861f

Please sign in to comment.