Skip to content

Commit

Permalink
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 authored and bosd committed Jul 12, 2024
1 parent c93720e commit d360bde
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
41 changes: 15 additions & 26 deletions base_ubl/models/ubl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,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 @@ -587,17 +581,14 @@ def _ubl_check_xml_schema(self, xml_string, document, version="2.1"):

@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 584 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L584

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

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

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L587-L588

Added lines #L587 - L588 were not covered by tests
pdf_content, xml_filename, xml_string
)
new_buffer = BytesIO()
writer.write(new_buffer)
new_buffer = BytesIO(new_content)

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

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L591

Added line #L591 was not covered by tests
return new_buffer

@api.model
Expand All @@ -607,16 +598,14 @@ def _embed_ubl_xml_in_pdf_content(self, xml_string, xml_filename, pdf_content):
-> it will return the new PDF binary with the embedded XML
(used for qweb-pdf reports)
"""
logger.warning(

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

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L601

Added line #L601 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 606 in base_ubl/models/ubl.py

View check run for this annotation

Codecov / codecov/patch

base_ubl/models/ubl.py#L606

Added line #L606 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 @@ -638,8 +627,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(
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 d360bde

Please sign in to comment.