Skip to Content
Menu
This question has been flagged
2 Replies
5979 Views

Just want to merge the content of more than one pdf into a single one and the header would be a combination of all pdf's header.

Avatar
Discard
Best Answer

Install the Python library PyPDF2, which provides functionality for manipulating PDFs. You can install it using pip:
pip install PyPDF2

  1. Create a Python function that merges the PDFs and adds the combined header. Here's an example:

import PyPDF2

def merge_pdfs_with_combined_header(pdf_paths, header_text, output_path):
merger = PyPDF2.PdfFileMerger()

# Read each PDF and add it to the merger object
for pdf_path in pdf_paths:
merger.append(pdf_path)

# Add the combined header to each page of the merged PDF
for page_num in range(merger.getNumPages()):
page = merger.getPage(page_num)
page.mergePage(PyPDF2.pdf.PageObject.createBlankPage(page))
header = PyPDF2.pdf.PageObject.createTextObject(header_text)
page.mergeTranslatedPage(header, 0, page.mediaBox.getHeight())

# Save the merged PDF with the combined header
merger.write(output_path)
merger.close()

Avatar
Discard
Best Answer

Hi,

If you are familiar with code , you can try use the following

attachment_report_id = self.env['ir.attachment'].sudo().create(
report_values)
# self.env.cr.commit()
merged_attach_ids = attachment_report_id._full_path(
attachment_report_id.store_fname)

pdfWriter = PyPDF2.PdfFileWriter()
for list in attachment_list:
pdf1File = open(list, 'rb')
pdf1Reader = PyPDF2.PdfFileReader(pdf1File)
#
for pageNum in range(pdf1Reader.numPages):
pageObj = pdf1Reader.getPage(pageNum)
pdfWriter.addPage(pageObj)
#
for page in range(new_pdf.numPages):
pageObj1 = new_pdf.getPage(page)
pdfWriter.addPage(pageObj1)

pdfOutputFile = open(merged_attach_ids, 'wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
pdf1File.close()

Regards

Avatar
Discard

@Cybroses
What are attachment_list, new_pdf variables in your code. Please make it clear.

Related Posts Replies Views Activity
11
Oct 24
26950
0
Oct 22
1807
0
Apr 22
857
0
Mar 21
1753
1
Feb 21
4262