Hi, my goal is to write text to a pdf. When I open the pdf I only see a white page. I think I have an error in my setup and not in my code. Below a minimal working example.
Q: How can I debug this, to find out what is going on?
Thanks, regards Edzo
Linux fedora 6.17.7-300.fc43.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Nov 2 15:30:09 UTC 2025 x86_64 GNU/Linux
Python 3.14.0
pymupdf 1.26.6
MWE
import pymupdf from pymupdf.utils import getColor # set defaults file_name = “PyMuPDF_test.pdf” text_color = getColor(“black”) font_size = 32.0 font_name = “spacemo” p = pymupdf.Point(10, 10) # debug reasons text = “Some text.” # create pdf newDocument = pymupdf.open() fmt = pymupdf.paper_rect(“a4”) newPage = newDocument.new_page( width = fmt.width, height = fmt.height) # write to new page newPage.clean_contents() newPage.insert_text = ( p, text, fontsize := font_size, fontname := font_name, color := text_color, border_width := 1, overlay := True ) # write to disk newDocument.save(file_name, garbage = 3, deflate = True,) newDocument.close()
I thought at the start also this was the issue, but with this code I recieve a strange error. I thought I sort of had a new version of test_insert that needed a different syntax. Apparantly that is not the issue.
I also tried to remove all the spaces and pot it on 1 line, same error.
So I am a bit stuck
@HaraldLieder : Thanks. I removed the line in the code below.
Error
$ python3 write_pdf.py File "******/write_pdf.py", line 20 fontsize = font_size, ^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? $
With code
import pymupdf from pymupdf.utils import getColor # set defaults file_name = "PyMuPDF_test.pdf" text_color = getColor("black") font_size = 32.0 font_name = "spacemo" p = pymupdf.Point(10, 10) # debug reasons text = "Some text." # create pdf newDocument = pymupdf.open() fmt = pymupdf.paper_rect("a4") newPage = newDocument.new_page(width = fmt.width, height = fmt.height) # write to new page newPage.insert_text = ( p, text, fontsize = font_size, fontname = font_name, color = text_color, border_width = 1, overlay = True ) # write to disk newDocument.save(file_name, garbage = 3, deflate = True,) newDocument.close()