Skip to content

'add_redact_annot' asking for 'need font file or buffer' for an existing font in PDF #3212

@santanuOUP

Description

@santanuOUP

Description of the bug

Trying to replace one text in a PDF using the following methods:
add_redact_annot
apply_redactions

Text replacement is done but the replaced text is not showing just like the searched text. So, wanted to apply the same font but PyMuPDF is throwing exception saying "need font file or buffer". Font for searched text was 'Verdana' so want to apply the same font for the replaced text.

Here is the log for the operation:

2024-02-29 11:42:34,188 Filename to process: Sample
2024-02-29 11:42:34,194 fonts: [(7, 'ttf', 'TrueType', 'FAAAAH+TimesNewRomanPSMT', 'FAAAAH', 'WinAnsiEncoding'), (11, 'ttf', 'TrueType', 'FAAABB+Verdana', 'FAAABB', 'WinAnsiEncoding'), (16, 'ttf', 'TrueType', 'FAAABG+Verdana-Bold', 'FAAABG', 'WinAnsiEncoding')]
2024-02-29 11:42:34,204 hits: [Rect(170.0, 252.3948974609375, 222.64393615722656, 265.7635498046875)]
2024-02-29 11:42:34,209 search_text: LocalPath
2024-02-29 11:42:34,209 search_text_size: 11.0 search_text_font: Verdana
2024-02-29 11:42:34,213 Exception raised: need font file or buffer

Here is my code:

try:
                # Open a document
                search_text = "LocalPath"
                replace_text = "better text"
                global search_text_size
                global search_text_font 
                with fitz.open(os.path.join("asset", file)) as doc:  
                    for page in doc:                  
                        logger.info(f'fonts: {page.get_fonts()}')
                        hits = page.search_for(search_text)  # list of rectangles where to replace
                        logger.info(f'hits: {hits}')
                        page_blocks = page.get_text("dict")["blocks"]
                        for block in page_blocks:
                            if "lines" in block.keys():
                                spans = block['lines']
                                for span in spans:
                                    data = span['spans']                                    
                                    for lines in data:
                                        if search_text == lines['text']:
                                            search_text_size = lines['size']
                                            search_text_font = lines['font']
                                            logger.info(f'search_text_size: {search_text_size} search_text_font: {search_text_font}')
                                                    
                        for rect in hits:
                            page.add_redact_annot(rect, replace_text, fontname = 'Verdana')  

                        page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE)  # don't touch images
                    doc.save("replaced.pdf", garbage=3, deflate=True)     
                
            except Exception as e:
                logger.error(f'Exception raised: {str(e)}')

Trying for long time, need help!

How to reproduce the bug

  1. Run the same code with a PDF file
  2. Try to replace one existing text and apply the same font like existing font to the replaced text

PyMuPDF version

1.23.25

Operating system

Windows

Python version

3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    not a bugnot a bug / user error / unable to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions