Skip to content

Commit

Permalink
[IMP] base_ubl: use pdf.helper.pdf_embed_xml
Browse files Browse the repository at this point in the history
Mark _ubl_add_xml_in_pdf_buffer as deprecated
Mark _embed_ubl_xml_in_pdf_content
  • Loading branch information
jbaudoux committed Dec 22, 2023
1 parent 18c7456 commit 9a90063
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
43 changes: 15 additions & 28 deletions base_ubl/models/ubl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

logger = logging.getLogger(__name__)

try:
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.generic import NameObject
except ImportError:
logger.debug("Cannot import PyPDF2")


class BaseUbl(models.AbstractModel):
_name = "base.ubl"
Expand Down Expand Up @@ -593,40 +587,33 @@ def _ubl_check_xml_schema(self, xml_string, document, version="2.1"):
) from e
return True

# TODO: move to pdf_helper
@api.model
def _ubl_add_xml_in_pdf_buffer(self, xml_string, xml_filename, buffer):
# Add attachment to PDF content.
reader = PdfFileReader(buffer)
writer = PdfFileWriter()
writer.appendPagesFromReader(reader)
writer.addAttachment(xml_filename, xml_string)
# show attachments when opening PDF
writer._root_object.update(
{NameObject("/PageMode"): NameObject("/UseAttachments")}
logger.warning(

Check warning on line 592 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L592

Added line #L592 was not covered by tests
"`_ubl_add_xml_in_pdf_buffer` deprecated: use `pdf.helper.pdf_embed_xml`"
)
new_buffer = BytesIO()
writer.write(new_buffer)
pdf_content = buffer.getvalue()
new_content = self.env["pdf.helper"].pdf_embed_xml(

Check warning on line 596 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L595-L596

Added lines #L595 - L596 were not covered by tests
pdf_content, xml_filename, xml_string
)
new_buffer = BytesIO(new_content)

Check warning on line 599 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L599

Added line #L599 was not covered by tests
return new_buffer

# TODO: move to pdf_helper
@api.model
def _embed_ubl_xml_in_pdf_content(self, xml_string, xml_filename, pdf_content):
"""Add the attachments to the PDF content.
Use the pdf_content argument, which has the binary of the PDF
-> it will return the new PDF binary with the embedded XML
(used for qweb-pdf reports)
"""
logger.warning(

Check warning on line 609 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L609

Added line #L609 was not covered by tests
"`_embed_ubl_xml_in_pdf_content` deprecated: use `pdf.helper.pdf_embed_xml`"
)
self.ensure_one()
logger.debug("Starting to embed %s in PDF", xml_filename)

with BytesIO(pdf_content) as reader_buffer:
buffer = self._ubl_add_xml_in_pdf_buffer(
xml_string, xml_filename, reader_buffer
)
pdf_content = buffer.getvalue()
buffer.close()

pdf_content = self.env["pdf.helper"].pdf_embed_xml(

Check warning on line 614 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L614

Added line #L614 was not covered by tests
pdf_content, xml_filename, xml_string
)
logger.info("%s file added to PDF content", xml_filename)
return pdf_content

Expand All @@ -649,8 +636,8 @@ def embed_xml_in_pdf(
if pdf_file:
with open(pdf_file, "rb") as f:
pdf_content = f.read()
updated_pdf_content = self._embed_ubl_xml_in_pdf_content(
xml_string, xml_filename, pdf_content
updated_pdf_content = self.env["pdf.helper"].pdf_embed_xml(

Check warning on line 639 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L639

Added line #L639 was not covered by tests
pdf_content, xml_filename, xml_string
)
if pdf_file:
with open(pdf_file, "wb") as f:
Expand Down
6 changes: 5 additions & 1 deletion pdf_helper/models/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging

from odoo import api, models
from odoo.tools.pdf import OdooPdfFileReader, OdooPdfFileWriter
from odoo.tools.pdf import NameObject, OdooPdfFileReader, OdooPdfFileWriter

from ..utils import PDFParser

Expand Down Expand Up @@ -42,5 +42,9 @@ def pdf_embed_xml(self, pdf_content, xml_filename, xml_string):
writer = OdooPdfFileWriter()
writer.cloneReaderDocumentRoot(reader)
writer.addAttachment(xml_filename, xml_string, subtype="text/xml")
# show attachments when opening PDF
writer._root_object.update(
{NameObject("/PageMode"): NameObject("/UseAttachments")}
)
writer.write(new_pdf_stream)
return new_pdf_stream.getvalue()

0 comments on commit 9a90063

Please sign in to comment.