Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
6277 Widoki

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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()

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć

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

Powiązane posty Odpowiedzi Widoki Czynność
11
paź 24
28134
0
paź 22
2035
0
kwi 22
857
0
mar 21
1977
1
lut 21
4558