SPB Git

Repository Public License

02-pdf-to-structured-report.yaml 5.1 KB 135 lines yaml

1# ORIX OS Automation2# File: Workflows/Examples/02-pdf-to-structured-report.yaml3# Description: Built-in automation: extracts metadata and text statistics from the PDFs of a folder into a CSV and a PDF report.4#5# Author: Simon-Pierre Boucher6# Contact: contact@spboucher.ai7#8# Limitations9# - Text extraction works on PDFs that carry a text layer; scanned PDFs yield no text (no OCR here) and are reported10#   with 0 text characters.11# - Only the first 200 000 bytes of each PDF's text are read; the character count in the report is the count of what was read.12# - The title is the PDF's own "Title" property; many PDFs have none and the column is then empty. A title containing13#   a | character breaks the Markdown table (not the CSV).14# - The report is a plain table; no model is involved, so nothing is "summarised".15# - At most 100 PDFs are found per run (the default bound of filesystem.search); encrypted PDFs fail their iteration.16apiVersion: orix.ai/v117kind: Automation18metadata:19  id: pdf-to-structured-report20  name: PDF to structured data and report21  description: >-22    Reads every PDF of a folder, extracts page count, title and the size of its text layer, writes the23    result as a CSV catalogue and renders a PDF report from a Markdown table.24  tags: [pdf, documents, report]25spec:26  triggers:27    - type: manual28  parameters:29    - name: inputFolder30      type: path31      default: ~/ORIX Samples/pdf-to-structured-report/Input32      description: Folder containing the PDFs.33    - name: outputFolder34      type: path35      default: ~/ORIX Samples/pdf-to-structured-report/Output36      description: Folder that receives catalog.csv, report.md and report.pdf.37  permissions:38    filesystem:39      read: ["${{ params.inputFolder }}", "${{ params.outputFolder }}"]40      write: ["${{ params.outputFolder }}"]41  budget:42    maxDurationSeconds: 90043    maxToolCalls: 100044  steps:45    - id: find46      name: Find the PDFs47      tool: filesystem.search48      with:49        path: ${{ params.inputFolder }}50        pattern: "*.pdf"51    - id: pdfs52      needs: [find]53      transform: ${{ filter(steps.find.output.matches, m => m.type == "file") }}54    - id: prepare55      name: Create the output folder56      tool: filesystem.mkdir57      with:58        path: ${{ params.outputFolder }}59    - id: extract60      name: Read each PDF61      needs: [pdfs]62      foreach: ${{ steps.pdfs.output }}63      as: pdf64      concurrency: 265      collect: row66      steps:67        - id: info68          tool: documents.pdfInfo69          with:70            path: ${{ pdf.path }}71        - id: text72          tool: documents.pdfText73          with:74            path: ${{ pdf.path }}75            maxBytes: 20000076        - id: row77          needs: [info, text]78          transform: ${{ [basename(pdf.path), steps.info.output.pageCount, default(steps.info.output.title, ""), sum(steps.text.output.pages, p => len(p.text))] }}79    - id: catalog80      name: Write the CSV catalogue81      needs: [extract, prepare]82      tool: documents.csvWrite83      with:84        path: ${{ params.outputFolder }}/catalog.csv85        columns: [file, pages, title, textCharacters]86        rows: ${{ steps.extract.output.results }}87        overwrite: true88    - id: compose89      name: Compose the report90      needs: [extract]91      transform: |92        ${{ "# PDF report\n\n"93            + "Folder: " + params.inputFolder + "\n\n"94            + "Generated: " + env.DATETIME + "\n\n"95            + "Documents: " + steps.extract.output.count + " — pages in total: " + sum(steps.extract.output.results, r => r[1]) + "\n\n"96            + "| File | Pages | Title | Text characters |\n|---|---|---|---|\n"97            + join(map(steps.extract.output.results, r => "| " + join(r, " | ") + " |"), "\n") + "\n" }}98    - id: markdown99      name: Write the Markdown report100      needs: [compose, prepare]101      tool: filesystem.write102      with:103        path: ${{ params.outputFolder }}/report.md104        overwrite: true105        content: ${{ steps.compose.output }}106    - id: render107      name: Render the report as PDF108      needs: [compose, prepare]109      tool: documents.renderPDF110      with:111        content: ${{ steps.compose.output }}112        destination: ${{ params.outputFolder }}/report.pdf113        overwrite: true114    - id: check115      name: Verify the artifacts116      needs: [catalog, markdown, render]117      verify:118        - truthy: ${{ steps.extract.output.count > 0 }}119        - equals: ["${{ steps.catalog.output.rows }}", "${{ steps.extract.output.count }}"]120        - fileNonEmpty: ${{ params.outputFolder }}/catalog.csv121        - fileContains: ${{ params.outputFolder }}/report.md122          text: "| File | Pages |"123        - fileNonEmpty: ${{ params.outputFolder }}/report.pdf124        - truthy: ${{ steps.render.output.pages >= 1 }}125    - id: result126      needs: [check]127      output:128        documents: ${{ steps.extract.output.count }}129        pages: ${{ sum(steps.extract.output.results, r => r[1]) }}130        catalog: ${{ params.outputFolder }}/catalog.csv131        report: ${{ params.outputFolder }}/report.pdf132      artifacts:133        - ${{ params.outputFolder }}/catalog.csv134        - ${{ params.outputFolder }}/report.pdf135
Files main
Menu

Theme