Skip to content

Commit

Permalink
release v2.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisschellekens committed Oct 31, 2021
1 parent 17e1208 commit a496503
Show file tree
Hide file tree
Showing 257 changed files with 705 additions and 138 deletions.
4 changes: 4 additions & 0 deletions borb/io/write/any_object_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from borb.io.read.types import AnyPDFType
from borb.io.write.ascii_art.ascii_art_transformer import ASCIIArtTransformer
from borb.io.write.document.document_transformer import DocumentTransformer
from borb.io.write.document.information_dictionary_transformer import (
InformationDictionaryTransformer,
)
from borb.io.write.image.image_transformer import ImageTransformer
from borb.io.write.object.array_transformer import ArrayTransformer
from borb.io.write.object.dictionary_transformer import DictionaryTransformer
Expand Down Expand Up @@ -45,6 +48,7 @@ def __init__(self):
self.add_child_transformer(XREFTransformer())
self.add_child_transformer(PagesTransformer())
self.add_child_transformer(PageTransformer())
self.add_child_transformer(InformationDictionaryTransformer())
# object types
self.add_child_transformer(ArrayTransformer())
self.add_child_transformer(StreamTransformer())
Expand Down
2 changes: 1 addition & 1 deletion borb/io/write/ascii_art/ascii_logo.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
borb version 2.0.12
borb version 2.0.13
Joris Schellekens
63 changes: 15 additions & 48 deletions borb/io/write/document/document_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
This implementation of WriteBaseTransformer is responsible for writing Document objects
"""
import datetime
import logging
import random
import typing
Expand All @@ -16,7 +15,6 @@
HexadecimalString,
List,
Name,
String,
)
from borb.io.write.transformer import (
Transformer,
Expand All @@ -38,6 +36,16 @@ def can_be_transformed(self, any: AnyPDFType):
"""
return isinstance(any, Document)

def _build_empty_document_info_dictionary(
self, object_to_transform: Dictionary
) -> None:
# create Info dictionary if needed
if "Info" not in object_to_transform["XRef"]["Trailer"]:
object_to_transform["XRef"]["Trailer"][Name("Info")] = Dictionary()
object_to_transform["XRef"]["Trailer"]["Info"].set_parent(
object_to_transform["XRef"]["Trailer"]
)

def transform(
self,
object_to_transform: Any,
Expand All @@ -58,65 +66,24 @@ def transform(
# invalidate all references
DocumentTransformer._invalidate_all_references(object_to_transform)

# create Info dictionary if needed
if "Info" not in object_to_transform["XRef"]["Trailer"]:
object_to_transform["XRef"]["Trailer"][Name("Info")] = Dictionary()

# set /ID
random_id = HexadecimalString("%032x" % random.randrange(16 ** 32))
if "ID" not in object_to_transform["XRef"]["Trailer"]:
object_to_transform["XRef"]["Trailer"][
Name("ID")
] = List().set_can_be_referenced( # type: ignore [attr-defined]
False
)
# fmt: off
object_to_transform["XRef"]["Trailer"][Name("ID")] = List().set_can_be_referenced(False) # type: ignore [attr-defined]
object_to_transform["XRef"]["Trailer"]["ID"].append(random_id)
object_to_transform["XRef"]["Trailer"]["ID"].append(random_id)
# fmt: on
else:
object_to_transform["XRef"]["Trailer"]["ID"][1] = random_id
object_to_transform["XRef"]["Trailer"]["ID"].set_can_be_referenced(False)

# set CreationDate
modification_date = DocumentTransformer._timestamp_to_str()
if "CreationDate" not in object_to_transform["XRef"]["Trailer"][Name("Info")]:
object_to_transform["XRef"]["Trailer"][Name("Info")][
Name("CreationDate")
] = String(modification_date)

# set ModDate
object_to_transform["XRef"]["Trailer"]["Info"][Name("ModDate")] = String(
modification_date
)

# set Producer
object_to_transform["XRef"]["Trailer"]["Info"][Name("Producer")] = String(
"borb"
)

# set OutputIntents
# fmt: off
rgb_output_intent: Dictionary = Dictionary()
rgb_output_intent[Name("Type")] = Name("OutputIntent")
rgb_output_intent[Name("S")] = Name("GTS_PDFA1")
rgb_output_intent[Name("OutputConditionIdentifier")] = String("sRGB")
rgb_output_intent[Name("RegistryName")] = String("http://www.color.org")
rgb_output_intent[Name("Info")] = String("Creator:HP Manufacturer:IEC Model:sRGB")
# object_to_transform["XRef"]["Trailer"]["Root"][Name("OutputIntents")] = List()
# object_to_transform["XRef"]["Trailer"]["Root"][Name("OutputIntents")].append(rgb_output_intent)
# fmt: on
# \Info
self._build_empty_document_info_dictionary(object_to_transform)

# transform XREF
self.get_root_transformer().transform(object_to_transform["XRef"], context)

@staticmethod
def _timestamp_to_str() -> str:
timestamp_str = "D:"
now = datetime.datetime.now()
for n in [now.year, now.month, now.day, now.hour, now.minute, now.second]:
timestamp_str += "{0:02}".format(n)
timestamp_str += "Z00"
return timestamp_str

@staticmethod
def _invalidate_all_references(object: AnyPDFType) -> None:
objects_done: typing.List[AnyPDFType] = []
Expand Down
Loading

0 comments on commit a496503

Please sign in to comment.