To avoid having to fixup half a dozen places where we're creating PDF writers, and possibly ending up with new ill-configured writers in the future, patch PyPDF2's own writer with a subclass setting /Creator and /Producer. Note that this will not affect non-post-processed PDFs generated by wkhtmltopdf. wkhtmltopdf does not allow setting these properties[0][1], so to fix this issue we'd have to alter _run_wkhtmltopdf to pass the result through PyPDF2 in order to alter its metadata. [0] https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2000 [1] https://bugreports.qt.io/browse/QTBUG-44451 closes odoo/odoo#29460 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
import io
|
|
import PyPDF2
|
|
|
|
class BrandedFileWriter(PyPDF2.PdfFileWriter):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.addMetadata({
|
|
'/Creator': "Odoo",
|
|
'/Producer': "Odoo",
|
|
})
|
|
PyPDF2.PdfFileWriter = BrandedFileWriter
|
|
|
|
def merge_pdf(pdf_data):
|
|
''' Merge a collection of PDF documents in one
|
|
:param list pdf_data: a list of PDF datastrings
|
|
:return: a unique merged PDF datastring
|
|
'''
|
|
writer = PyPDF2.PdfFileWriter()
|
|
for document in pdf_data:
|
|
reader = PyPDF2.PdfFileReader(io.BytesIO(document), strict=False)
|
|
for page in range(0, reader.getNumPages()):
|
|
writer.addPage(reader.getPage(page))
|
|
with io.BytesIO() as _buffer:
|
|
writer.write(_buffer)
|
|
return _buffer.getvalue()
|
|
|
|
|
|
def rotate_pdf(pdf):
|
|
''' Rotate clockwise PDF (90°)
|
|
:param pdf: a PDF to rotate
|
|
:return: a PDF rotated
|
|
'''
|
|
writer = PyPDF2.PdfFileWriter()
|
|
reader = PyPDF2.PdfFileReader(io.BytesIO(pdf), strict=False)
|
|
for page in range(0, reader.getNumPages()):
|
|
page = reader.getPage(page)
|
|
page.rotateClockwise(90)
|
|
writer.addPage(page)
|
|
with io.BytesIO() as _buffer:
|
|
writer.write(_buffer)
|
|
return _buffer.getvalue()
|