MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / scrub

Function scrub

src_classic/utils.py:4146–4296  ·  view source on GitHub ↗
(
    doc: Document,
    attached_files: bool = True,
    clean_pages: bool = True,
    embedded_files: bool = True,
    hidden_text: bool = True,
    javascript: bool = True,
    metadata: bool = True,
    redactions: bool = True,
    redact_images: int = 0,
    remove_links: bool = True,
    reset_fields: bool = True,
    reset_responses: bool = True,
    thumbnails: bool = True,
    xml_metadata: bool = True,
)

Source from the content-addressed store, hash-verified

4144# Acrobat 'sanitize' function
4145# ------------------------------------------------------------------------------
4146def scrub(
4147 doc: Document,
4148 attached_files: bool = True,
4149 clean_pages: bool = True,
4150 embedded_files: bool = True,
4151 hidden_text: bool = True,
4152 javascript: bool = True,
4153 metadata: bool = True,
4154 redactions: bool = True,
4155 redact_images: int = 0,
4156 remove_links: bool = True,
4157 reset_fields: bool = True,
4158 reset_responses: bool = True,
4159 thumbnails: bool = True,
4160 xml_metadata: bool = True,
4161) -> None:
4162 def remove_hidden(cont_lines):
4163 """Remove hidden text from a PDF page.
4164
4165 Args:
4166 cont_lines: list of lines with /Contents content. Should have status
4167 from after page.cleanContents().
4168
4169 Returns:
4170 List of /Contents lines from which hidden text has been removed.
4171
4172 Notes:
4173 The input must have been created after the page's /Contents object(s)
4174 have been cleaned with page.cleanContents(). This ensures a standard
4175 formatting: one command per line, single spaces between operators.
4176 This allows for drastic simplification of this code.
4177 """
4178 out_lines = [] # will return this
4179 in_text = False # indicate if within BT/ET object
4180 suppress = False # indicate text suppression active
4181 make_return = False
4182 for line in cont_lines:
4183 if line == b"BT": # start of text object
4184 in_text = True # switch on
4185 out_lines.append(line) # output it
4186 continue
4187 if line == b"ET": # end of text object
4188 in_text = False # switch off
4189 out_lines.append(line) # output it
4190 continue
4191 if line == b"3 Tr": # text suppression operator
4192 suppress = True # switch on
4193 make_return = True
4194 continue
4195 if line[-2:] == b"Tr" and line[0] != b"3":
4196 suppress = False # text rendering changed
4197 out_lines.append(line)
4198 continue
4199 if line == b"Q": # unstack command also switches off
4200 suppress = False
4201 out_lines.append(line)
4202 continue
4203 if suppress and in_text: # suppress hidden lines

Callers

nothing calls this directly

Calls 15

remove_hiddenFunction · 0.85
set_metadataMethod · 0.80
widgetsMethod · 0.80
get_linksMethod · 0.80
delete_linkMethod · 0.80
annotsMethod · 0.80
update_fileMethod · 0.80
delete_responsesMethod · 0.80
apply_redactionsMethod · 0.80
get_contentsMethod · 0.80
xref_streamMethod · 0.80
update_streamMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…