A PDF document, such as a form, a report or a scanned record. Its text is read from the page images, so a scan reads like any other PDF: read it by page and line, and look at a page as a picture when the layout matters.
At a glance
- Stored as: a handle (
pdf-…), kept once by its content and referred to by that handle. - Read in parts: by a range of pages.
- Media formats:
pdf,png,jpeg. - Edited by: filling its fields and drawing on it.
- Form fields: its fillable fields can be found and filled.
- Search: by passage, so full-text and similarity search rank a value by its best passage.
Writing
- Send the file itself: upload it, or give create its
file, its bytes, an http(s) URL or adata:URI. A picture (JPEG, PNG or another image format) becomes a one-page PDF; other content that is not a PDF is refused, and so is plain text without the render option below. - To make a new PDF, call create with
opts{"kind": "render"}and the content assource, or the handle of a markdown value, which is rendered as markdown. Text is rendered as SVG when it starts with<svgor<?xml, as HTML when it starts with a tag, as markdown when it has#headings,*bullets, bold or links, and as plain text otherwise. Plain text is set in a basic font in which characters outside Western European alphabets print as?; write markdown for anything richer.
Written as
A JSON object.
__dtype__: str
Create options
Options for rendering markdown, HTML, SVG or plain text as a new PDF.
kind: optional "render". Selects rendering; source is markdown, HTML, SVG or text.
Reading
- get_content reads pages:
rangecounts pages from 1, inclusive ([3, 4],"8-"). Every line is labelledpage, line:, and the line number runs on through the whole document rather than restarting on each page:12, 340:is line 340, on page 12. querysearches the text (withrange, only those pages). Hits keep their labels, so the next call can read the page around one. A read longer thanmax_charsstops at a page boundary and ends with the call that reads on.- The text is read the first time anyone asks for it, or by the search index shortly after the PDF is saved. The first read of a long document takes a while.
- get_media with
formatpngorjpegreturns the pages ofrangeas images, for what text cannot show: layout, stamps, signatures, ticks and handwriting. Ask only for the pages you need; a long range is cut, with a note saying so. - With the default
format, get_media stores the pages ofrangeas a new PDF and returns its handle: the way to pass a few pages of a long document on. get_content withas_handlekeeps the text of those pages instead.
Querying
The text is indexed in passages, so the search operators find a PDF by what it says,
scanned ones included once their text has been read, a little after the save (see the
HQL reference). For a PDF, match.start and match.end count
lines of the document's text from 0 (one less than the line in a page, line: label),
not pages.
Form | pdf_copy ~~ "payment terms" | select id, match.text, match.start | limit 10
Message | ~* "invoice" | match.dtype = "pdf" | select id, match.field, match.text | limit 20
content.dtype asks whether a message carries a PDF at all, without searching:
Message | content.dtype = "pdf" | select id, created_at | limit 50
size.pages counts the PDF's pages. Like the text, it arrives after the save; while the
PDF's own page count is not known yet, it counts up to the last page with text:
Form | pdf_copy.size.pages > 10 | order by pdf_copy.size.pages desc | select id, pdf_copy.size{pages,bytes}
Editing
- extract_form_fields finds the fields: the PDF's own fillable fields, and fields read from the page images, which covers scans (a paid call). Each has a stable id, its page, and its box as fractions (0 to 1) of the page.
- edit with
filledits writes a value into a field by its id: into the PDF's own field when there is one, else as text set in the field's box.trueticks a checkbox. drawedits add text, a picture (its handle) or SVG in a box[x, y, w, h], fractions of the page measured from its top left. Use SVG for lines, shapes and ticks.flattenbakes the fields and drawings into the page, so they are no longer editable.- The result is a new PDF under a new handle; the original is unchanged. An edit that cannot be applied (an unknown field id, a box off the page) is listed as skipped and the others are applied.
In agent tools
- Pass the handle or its
<attachment id="…"/>marker. A generic file whose content is a PDF is accepted too. - In a conversation the agent is shown the marker and the text with its
page, line:labels; for a long document, its first and last lines and a note on how to read the rest by page. - The agent reads further with get_content, and looks at a page with get_media only when the layout matters.
Metadata
Derived values kept with a stored value:
ocr: The document's text as read from its pages, scanned pages included, one line per line. Read the first time the text is asked for, or by the search index shortly after the document is stored.form_fields: The form fields found by reading the page images, each with itskey(label),value,checkboxstate, positions as fractions of the page, and its 0-basedpage. Kept the first time the form fields are extracted.pdf_header: Facts about the file:num_pages,title,authorandsubject(from the file's own properties, null when absent),sizein bytes, andhas_widgetsandwidget_countfor its fillable form fields. Worked out the first time it is read.first_page_thumbnail_jpeg: A small JPEG of the first page, at most 240 pixels on its longer side, base64-encoded. Made the first time a preview needs it.text_excerpt: Up to 2,000 characters from the start of the document: its text when that has been read, else the text layer of its first pages (a scanned page adds nothing). Made the first time a preview needs it.page_starts: Where each page begins in that text: one number per page, the 0-based index of the page's first line. Written with the text.
Operations
Through the dtypes resource: