Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade pypdf lib and fix generation issues #6

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drafthorse/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "2.2.1"
version = "2.2.2"
33 changes: 18 additions & 15 deletions drafthorse/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import datetime
import logging
import os
from io import BytesIO
from lxml import etree
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import (
from pypdf import PdfReader, PdfWriter
from pypdf.generic import (
ArrayObject,
DecodedStreamObject,
DictionaryObject,
NameObject,
createStringObject,
create_string_object,
)

logger = logging.getLogger("drafthorse")


def attach_xml(original_pdf, xml_data, level="BASIC"):
if not isinstance(original_pdf, bytes):
Expand Down Expand Up @@ -71,13 +74,13 @@ def _get_original_output_intents(original_pdf):
pdf_root = original_pdf.trailer["/Root"]
ori_output_intents = pdf_root["/OutputIntents"]
for ori_output_intent in ori_output_intents:
ori_output_intent_dict = ori_output_intent.getObject()
ori_output_intent_dict = ori_output_intent.get_object()
dest_output_profile_dict = ori_output_intent_dict[
"/DestOutputProfile"
].getObject()
].get_object()
output_intents.append((ori_output_intent_dict, dest_output_profile_dict))
except: # noqa
pass
except Exception as ex:
logger.error(ex)
return output_intents


Expand Down Expand Up @@ -138,7 +141,7 @@ def _prepare_pdf_metadata_xml(level, pdf_metadata):
desc_adobe = etree.SubElement(rdf, ns_rdf + "Description", nsmap=nsmap_pdf)
desc_adobe.set(ns_rdf + "about", "")
producer = etree.SubElement(desc_adobe, ns_pdf + "Producer")
producer.text = "PyPDF2"
producer.text = "pypdf"
desc_xmp = etree.SubElement(rdf, ns_rdf + "Description", nsmap=nsmap_xmp)
desc_xmp.set(ns_rdf + "about", "")
creator = etree.SubElement(desc_xmp, ns_xmp + "CreatorTool")
Expand Down Expand Up @@ -192,14 +195,14 @@ def _facturx_update_metadata_add_attachment(
pdf_filestream, facturx_xml_str, pdf_metadata, facturx_level, output_intents
):
# md5sum = hashlib.md5(facturx_xml_str).hexdigest()
# md5sum_obj = createStringObject(md5sum)
# md5sum_obj = create_string_object(md5sum)
pdf_date = datetime.datetime.utcnow().strftime("D:%Y%m%d%H%M%SZ")
params_dict = DictionaryObject(
{
# NameObject('/CheckSum'): md5sum_obj,
NameObject("/ModDate"): createStringObject(pdf_date),
NameObject("/CreationDate"): createStringObject(pdf_date),
NameObject("/Size"): NameObject(str(len(facturx_xml_str))),
NameObject("/ModDate"): create_string_object(pdf_date),
NameObject("/CreationDate"): create_string_object(pdf_date),
NameObject("/Size"): create_string_object(str(len(facturx_xml_str))),
}
)
file_entry = DecodedStreamObject()
Expand All @@ -218,13 +221,13 @@ def _facturx_update_metadata_add_attachment(
{NameObject("/F"): file_entry_obj, NameObject("/UF"): file_entry_obj}
)

fname_obj = createStringObject("factur-x.xml")
fname_obj = create_string_object("factur-x.xml")
filespec_dict = DictionaryObject(
{
NameObject("/AFRelationship"): NameObject(
"/Data" if facturx_level in ("BASIC-WL", "MINIMUM") else "/Alternative"
),
NameObject("/Desc"): createStringObject(
NameObject("/Desc"): create_string_object(
"Invoice metadata conforming to ZUGFeRD standard (http://www.ferd-net.de/front_content.php?idcat=231&lang=4)"
),
NameObject("/Type"): NameObject("/Filespec"),
Expand All @@ -240,7 +243,7 @@ def _facturx_update_metadata_add_attachment(
)
name_arrayobj_content_final = []
af_list = []
for (fname_obj, filespec_obj) in name_arrayobj_content_sort:
for fname_obj, filespec_obj in name_arrayobj_content_sort:
name_arrayobj_content_final += [fname_obj, filespec_obj]
af_list.append(filespec_obj)
embedded_files_names_dict = DictionaryObject(
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lxml
PyPDF2
pypdf
pytest
flake8
isort
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"Programming Language :: Python :: 3.10",
],
keywords="xml banking sepa",
install_requires=["lxml", "PyPDF2"],
install_requires=["lxml", "pypdf"],
packages=find_packages(include=["drafthorse", "drafthorse.*"]),
include_package_data=True,
)