| 6561 | # Acrobat 'sanitize' function |
| 6562 | # ------------------------------------------------------------------------------ |
| 6563 | def scrub( |
| 6564 | doc: 'Document', |
| 6565 | attached_files: bool = True, |
| 6566 | clean_pages: bool = True, |
| 6567 | embedded_files: bool = True, |
| 6568 | hidden_text: bool = True, |
| 6569 | javascript: bool = True, |
| 6570 | metadata: bool = True, |
| 6571 | redactions: bool = True, |
| 6572 | redact_images: int = 0, |
| 6573 | remove_links: bool = True, |
| 6574 | reset_fields: bool = True, |
| 6575 | reset_responses: bool = True, |
| 6576 | thumbnails: bool = True, |
| 6577 | xml_metadata: bool = True, |
| 6578 | ) -> None: |
| 6579 | |
| 6580 | def remove_hidden(cont_lines): |
| 6581 | """Remove hidden text from a PDF page. |
| 6582 | |
| 6583 | Args: |
| 6584 | cont_lines: list of lines with /Contents content. Should have status |
| 6585 | from after page.cleanContents(). |
| 6586 | |
| 6587 | Returns: |
| 6588 | List of /Contents lines from which hidden text has been removed. |
| 6589 | |
| 6590 | Notes: |
| 6591 | The input must have been created after the page's /Contents object(s) |
| 6592 | have been cleaned with page.cleanContents(). This ensures a standard |
| 6593 | formatting: one command per line, single spaces between operators. |
| 6594 | This allows for drastic simplification of this code. |
| 6595 | """ |
| 6596 | out_lines = [] # will return this |
| 6597 | in_text = False # indicate if within BT/ET object |
| 6598 | suppress = False # indicate text suppression active |
| 6599 | make_return = False |
| 6600 | for line in cont_lines: |
| 6601 | if line == b"BT": # start of text object |
| 6602 | in_text = True # switch on |
| 6603 | out_lines.append(line) # output it |
| 6604 | continue |
| 6605 | if line == b"ET": # end of text object |
| 6606 | in_text = False # switch off |
| 6607 | out_lines.append(line) # output it |
| 6608 | continue |
| 6609 | if line == b"3 Tr": # text suppression operator |
| 6610 | suppress = True # switch on |
| 6611 | make_return = True |
| 6612 | continue |
| 6613 | if line[-2:] == b"Tr" and line[0] != b"3": |
| 6614 | suppress = False # text rendering changed |
| 6615 | out_lines.append(line) |
| 6616 | continue |
| 6617 | if line == b"Q": # unstack command also switches off |
| 6618 | suppress = False |
| 6619 | out_lines.append(line) |
| 6620 | continue |