Hi all,
I’m looking for a way to extract pages from a PDF based on whether they contain a specific text string.
The text string would be variable and could appear on one or multiple pages in the incoming PDF. If a page contains the matching text, I would like that page to be extracted or saved as a separate PDF.
Has anyone solved something similar in Enfocus Switch, PitStop Server, or using another workflow component?
Any suggestions would be appreciated.
Thanks!
Keep only PDF pages containing the text "ABC"
- magnussandstrom
- Advanced member
- Posts: 579
- Joined: Thu Jul 30, 2020 6:34 pm
- Location: Sweden
- Contact:
- JimmyHartington
- Advanced member
- Posts: 530
- Joined: Tue Mar 22, 2011 7:38 am
Re: Keep only PDF pages containing the text "ABC"
My idea would be to split the pdf.
Have an action list with "Select text containg" the variable.
Log the selection as error.
Output the errors from Pitstop Server configurator.
Have an action list with "Select text containg" the variable.
Log the selection as error.
Output the errors from Pitstop Server configurator.
Re: Keep only PDF pages containing the text "ABC"
Try "Strategy - Action List" of this app: https://www0.enfocus.com/en/appstore/pr ... -pdf-pages. Make sure to read the documentation because the Action List has to have a specific behavior.
Re: Keep only PDF pages containing the text "ABC"
Or you can run this action list, then split the remaining pages.
Laurent De Wilde, Solution Architect @ Enfocus
- magnussandstrom
- Advanced member
- Posts: 579
- Joined: Thu Jul 30, 2020 6:34 pm
- Location: Sweden
- Contact:
Re: Keep only PDF pages containing the text "ABC"
Solved it with a Python script and Run Command, fast and easy:
Code: Select all
#!/usr/bin/env python3
import sys
import re
import fitz
from pathlib import Path
def normalize_text(value: str) -> str:
return " ".join(value.split())
def main() -> int:
if len(sys.argv) != 4:
print("Usage: extract_printvis_assignment.py input.pdf output.pdf SOR414620-2", file=sys.stderr)
return 1
input_pdf = sys.argv[1]
output_pdf = sys.argv[2]
target_order_no = sys.argv[3].strip()
if not target_order_no:
print("ERROR: Empty target order number", file=sys.stderr)
return 1
pattern = re.compile(
r"Order\s+No\.?\s+" + re.escape(target_order_no),
re.IGNORECASE
)
matched_pages = []
try:
source_doc = fitz.open(input_pdf)
output_doc = fitz.open()
for page_index in range(source_doc.page_count):
page = source_doc.load_page(page_index)
# Search only in the top part of the page for performance.
page_rect = page.rect
header_rect = fitz.Rect(
0,
0,
page_rect.width,
page_rect.height * 0.18
)
page_text = page.get_text("text", clip=header_rect)
normalized_text = normalize_text(page_text)
if pattern.search(normalized_text):
output_doc.insert_pdf(
source_doc,
from_page=page_index,
to_page=page_index
)
matched_pages.append(page_index + 1)
if not matched_pages:
print(f"ERROR: Did not find Order No. {target_order_no}", file=sys.stderr)
return 3
Path(output_pdf).parent.mkdir(parents=True, exist_ok=True)
output_doc.save(output_pdf, garbage=3, deflate=True)
output_doc.close()
source_doc.close()
print(f"Extracted Order No. {target_order_no}")
print(f"Pages: {','.join(map(str, matched_pages))}")
return 0
except Exception as e:
print(f"ERROR: Failed while processing PDF: {e}", file=sys.stderr)
return 4
if __name__ == "__main__":
sys.exit(main())