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 c232683 commit 7debb96
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 @@ -592,40 +586,33 @@ def _ubl_check_xml_schema(self, xml_string, document, version="2.1"):
)
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(
"`_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(
pdf_content, xml_filename, xml_string
)
new_buffer = BytesIO(new_content)
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(
"`_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(
pdf_content, xml_filename, xml_string
)
logger.info("%s file added to PDF content", xml_filename)
return pdf_content

Expand All @@ -648,8 +635,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 7debb96

Please sign in to comment.