Hi people,
I’m currently facing an issue while attempting to add a watermark to a PDF file within a Frappe DocType. I’ve followed the guidance provided in the forum discussions and tried various solutions, but I’m still encountering an error.
Issue Description:
- I have a Frappe DocType with a file field for uploading PDF files.
- When attempting to add a watermark to the PDF file upon saving the document, I’m encountering the following error: "No such file or directory: ‘path to the uploaded file’ ".
Steps Taken:
- I’ve verified that the file paths are correct and the files actually exist.
- Adjusted the code based on community suggestions, including using Frappe’s File Manager.
Code Snippets:
def get_file_path(file_name):
public_path = frappe.get_site_path('public')
file_path = os.path.join(public_path, 'files', file_name)
return file_path
def add_watermark(pdf_path, watermark_text, output_path):
try:
with open(pdf_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
pdf_writer = PyPDF2.PdfFileWriter()
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
watermark = fitz.TextWatermark()
watermark.set_font(fitz.Font(fitz.FONT_HELVETICA), 36)
watermark.set_text(watermark_text)
watermark.set_pos((page.media_box[2] / 2, page.media_box[3] / 2))
page.insert_watermark(watermark)
pdf_writer.addPage(page)
with open(output_path, 'wb') as output_file:
pdf_writer.write(output_file)
except Exception as e:
frappe.msgprint(f"Error adding watermark: {str(e)}")
class PDFTest(Document):
def validate(self):
# pdf_path = get_file_path(self.file)
public_path = frappe.get_site_path('public')
pdf_path = os.path.join(public_path, 'files', self.file)
print(f'\n\n\n\n{pdf_path}\n\n\n\n')
watermark_text = "some texts"
output_path = get_file_path("watermark_added.pdf")
add_watermark(pdf_path, watermark_text, output_path)
I would appreciate any guidance or insights from the community regarding this issue. If anyone has successfully implemented watermarking in a Frappe DocType or can provide suggestions on handling file paths, it would be immensely helpful.
Thank you for your time and assistance!