--- name: pdf description: Reads, searches, compares, splits, merges and generates PDF files, answering questions with the page number and quoted wording behind each answer. Use when a user says "where is the termination clause", "compare these two contracts", "pull the tables out of this PDF", "merge these files", "split pages 3-7", "OCR this scan", or attaches a PDF. --- # PDF Works with PDFs the way a careful paralegal would: find the passage, quote it, give the page, and say when the file itself is the problem. Decide first whether the job is reading, changing, or creating. ## When to use this - The user attaches a PDF and asks a specific question about its content. - Two versions of a document need comparing. - Text or tables need extracting into a usable form. - Pages need merging, splitting, rotating or reordering. - A new PDF is needed, such as a report, certificate or formatted summary. ## Procedure 1. **Identify the job and the question.** For reading jobs, restate the question in one line so the search is targeted. For comparisons, confirm which file is the baseline. 2. **Inspect the file before extracting.** Check page count, whether pages contain selectable text, whether the file is encrypted, and whether it has form fields. A page with images but no text layer is a scan and needs OCR. 3. **Extract text with a layout-aware tool.** In Python, pdfplumber suits text and tables; pypdf suits page operations and metadata. Keep the page number attached to every extracted chunk from the start, because you will need it for citations. 4. **Use OCR only when needed.** For scanned pages, render them to images at about 300 dpi and run an open-source OCR engine such as Tesseract. Tell the user the text came from OCR and flag low-confidence passages, numbers and names especially. 5. **Answer with evidence.** Quote the exact wording in quotation marks, give the page number, and add a one-sentence plain-language reading. If the search finds nothing, say that, and list the terms you tried; do not infer an answer from absence. 6. **Compare documents structurally.** Align by heading or clause number first, not by raw page position. Report each change as: location in both files, old wording, new wording, and whether it shifts obligation, money, dates or scope. Separate substantive changes from formatting and typo changes. 7. **Extract tables carefully.** Export to CSV or a markdown table, spot-check three cells against the page, and warn about merged cells or tables that run across pages. 8. **For page operations,** write output to a new file, then reopen it and confirm the page count and first and last page content. Preserve bookmarks and metadata when practical. 9. **For generating a PDF,** build it with a library such as reportlab or by rendering HTML, set a readable base font size, add page numbers, and check the rendered pages for clipped text. 10. **Close with a short summary** naming any limits: unreadable pages, uncertain OCR, password protection or missing fonts. ## Output format ```markdown **Answer:** **Source:** p. — "" **Confidence:** ``` For comparisons, use a table with columns: Clause or section, File A, File B, Impact. For generated or modified files, give the saved path and page count. ## Quality checks Before answering, confirm each of these: - Every quote was copied from extracted text and checked against the page it cites. - Page numbers refer to the PDF's own page order, and a note says so if printed page labels differ. - Numbers read by OCR were double-checked, since digits are the most common OCR error. - Both files were read in full before any claim that something is absent. - Output files open, have the expected page count and keep readable text. ## Guardrails - Never alter the original file; write new files with new names. - Do not paraphrase when the user needs the exact wording; quote it. - Do not give legal, medical or financial advice from a document; report what it says and suggest professional review for decisions. - Do not bypass passwords or protections the user has not authorized you to remove. - Handle personal data in the file sparingly and keep it out of examples. - If a form cannot be filled reliably because of poor scan quality, say so rather than guessing at field contents.