Docgen
Fill Word templates from query rows to produce invoices, letters and other DOCX or PDF documents
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/docgen/index.md to help me query Docgen with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
See what you can query
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer
| customer | bytes | is_docx |
|---|---|---|
| Ada Lovelace | 36,649 | true |
| Alan Turing | 36,647 | true |
| Grace Hopper | 36,646 | true |
Quick start
Choose where to run Docgen, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'docgen' AS docgen (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-docgen'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'docgen' AS docgen (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-docgen:0.5.0'
);
3. Run a query
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'docgen' AS docgen
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer
Query Farm operates Docgen for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use docgen as the catalog alias for the query below.
4. Run a query
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach with uvx
Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.
ATTACH 'docgen' AS docgen (
TYPE vgi,
LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-docgen'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'docgen' AS docgen (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-docgen:0.5.0'
);
Overview
Mail-merge SQL data into DOCX templates -> filled documents (DOCX/PDF) as BLOBs.
Turn DuckDB query results into finished Word documents and PDFs — generate invoices, contracts, statements, and letters straight from SQL with mail-merge templates, no application code required.
docgen is a VGI worker that does the inverse of text-extraction tools such as vgi-tika and vgi-pdf: rather than pulling text and structure out of documents, it pushes your SQL row data into DOCX (Microsoft Word) templates to produce filled, ready-to-send documents. Each rendered document is returned to DuckDB as a BLOB, so you can write it to a file, store it in a table, or stream it onward. It is built for anyone who needs to automate document production at query time — billing and finance teams generating invoices and account statements, operations teams producing contracts and form letters, and developers building reporting or notification pipelines directly on their data warehouse.
Rendering is powered by docxtpl (documentation), which layers the Jinja2 templating engine over python-docx (documentation). You design a normal .docx template in Word using {{ field }} placeholders and {% for %} loops, and every field or column from your query becomes a template variable. Multi-document output is stitched together with docxcompose, and optional DOCX-to-PDF conversion is handled by a headless LibreOffice when it is available on the host (PDF is always optional and never a hard dependency).
The catalog exposes two functions. The scalar docgen_render(template, data[, pdf]) renders one document per row: pass a template — either a VARCHAR path (resolved directly, then under $VGI_DOCGEN_TEMPLATES) or inline .docx BLOB bytes — and a STRUCT of fields that become the Jinja2 variables, with an optional pdf flag. The table function docgen_merge(relation, template := ..., [pdf := true]) consumes an entire relation and concatenates one render per input row into a single combined document, ideal for batch statement runs or multi-page contract packs.
The sample_templates discovery table lists every template the worker ships along with its absolute path and the placeholder fields it expects, so an agent can produce a real document without supplying a file of its own.
Untrusted bytes are magic-checked before rendering, and bad input degrades cleanly to NULL (scalar) or a visible error (merge).
Catalog
Everything docgen exposes once attached — read straight from the worker's
released build.
main 1 table · 2 functions
Merge SQL data into DOCX templates -> filled documents (DOCX/PDF) as BLOBs
Schema examples (5)
Look up the bundled sample_invoice template's path in sample_templates and render one DOCX invoice per customer row, reporting each document's size and whether it carries the DOCX (ZIP 'PK') magic.
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer;
| customer | bytes | is_docx |
|---|---|---|
| Ada Lovelace | 36,649 | true |
| Alan Turing | 36,647 | true |
| Grace Hopper | 36,646 | true |
Discover the bundled sample_invoice template and render a filled preview from its path in the same query, alongside the placeholder fields it expects.
SELECT t.name, t.fields,
octet_length(docgen.main.docgen_render(t.path, {customer: 'Ada Lovelace', total: '99.50'})) > 0 AS rendered
FROM docgen.main.sample_templates AS t
WHERE t.name = 'sample_invoice'
ORDER BY t.name;
Non-DOCX inline bytes degrade to a clean NULL rather than crashing the worker.
SELECT docgen.main.docgen_render('not a docx'::BLOB, {customer: 'Ada'}) AS doc;
Request PDF output (pdf=true) from the bundled sample template via headless LibreOffice; NULL when LibreOffice is absent.
SELECT docgen.main.docgen_render((SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), {customer: 'Ada', total: '99.50'}, true) AS doc;
1 more example
Merge a whole relation into one document; an empty input relation yields zero rows.
SELECT doc
FROM docgen.main.docgen_merge((SELECT 'Ada' AS customer WHERE false), template := 'invoice.docx');
sample_templates
The DOCX templates the worker ships, with their path and template variables (discovery table).
List every bundled template with its path and the placeholder fields it expects.
SELECT name, path, fields FROM docgen.main.sample_templates ORDER BY name;
Count how many document templates the worker ships out of the box.
SELECT count(*) AS template_count FROM docgen.main.sample_templates;
Discover the bundled sample_invoice template and render a filled preview from its path in the same query, alongside the placeholder fields it expects.
SELECT t.name, t.fields,
octet_length(docgen.main.docgen_render(t.path, {customer: 'Ada Lovelace', total: '99.50'})) > 0 AS rendered
FROM docgen.main.sample_templates AS t
WHERE t.name = 'sample_invoice'
ORDER BY t.name;
docgen_render (template VARCHAR, data ANY, pdf BOOLEAN) +3 overloads
Render one DOCX document per row from a template (VARCHAR path, resolved under $VGI_DOCGEN_TEMPLATES) filled with a STRUCT of row data; returns the rendered .docx as a BLOB, or NULL on failure.
docgen_render(template VARCHAR, data ANY) → BLOBdocgen_render(template VARCHAR, data ANY, pdf BOOLEAN) → BLOBdocgen_render(template BLOB, data ANY) → BLOBdocgen_render(template BLOB, data ANY, pdf BOOLEAN) → BLOB
Look up the bundled sample_invoice template's path in sample_templates and render one DOCX invoice per customer row, reporting each document's size and whether it carries the DOCX (ZIP 'PK') magic.
WITH tpl AS (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), invoices AS (SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), docs AS (SELECT i.customer, docgen.main.docgen_render(tpl.path, {customer: i.customer, total: i.total}) AS doc FROM invoices AS i CROSS JOIN tpl) SELECT customer, octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx
FROM docs
ORDER BY customer;
| customer | bytes | is_docx |
|---|---|---|
| Ada Lovelace | 36,649 | true |
| Alan Turing | 36,647 | true |
| Grace Hopper | 36,646 | true |
Render an invoice from the bundled sample template and confirm a non-empty DOCX BLOB.
SELECT octet_length(docgen.main.docgen_render((SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), {customer: 'Ada', total: '99.50'})) > 0 AS ok;
Request PDF output (pdf=true) from the bundled sample template via headless LibreOffice; NULL when LibreOffice is absent.
SELECT docgen.main.docgen_render((SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), {customer: 'Ada', total: '99.50'}, true) AS doc;
5 more examples
Render from inline template BLOB bytes; non-DOCX bytes yield a clean NULL.
SELECT docgen.main.docgen_render('not a docx'::BLOB, {customer: 'Ada'}) AS doc;
Render inline template bytes to PDF (requires LibreOffice on PATH; NULL otherwise).
SELECT docgen.main.docgen_render('not a docx'::BLOB, {total: '99.50'}, true) AS doc;
SELECT octet_length(docgen.main.docgen_render((SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), {customer: 'Ada Lovelace', total: '99.50'})) > 0 AS ok;
SELECT docgen.main.docgen_render(NULL::VARCHAR, {customer: 'Ada'}) AS doc;
SELECT docgen.main.docgen_render('not a docx'::BLOB, {x: 1}) AS doc;
docgen_merge (data TABLE, pdf := BOOLEAN, template := VARCHAR)
Mail-merge: render a DOCX 'template' once per input row and concatenate into ONE merged document (page break between rows), returned as a single BLOB. With pdf:=true, convert via headless LibreOffice. Every relation column is a template variable.
SET VARIABLE tpl = (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice');
SELECT octet_length(doc) > 0 AS ok FROM docgen.main.docgen_merge((SELECT * FROM (VALUES ('Ada', '99.50'), ('Grace', '42.00')) AS t(customer, total)), template := getvariable('tpl'));
SET VARIABLE tpl = (SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice');
SELECT octet_length(doc) AS bytes, doc[1:2] = 'PK'::BLOB AS is_docx FROM docgen.main.docgen_merge((SELECT * FROM (VALUES ('Ada Lovelace', '99.50'), ('Grace Hopper', '42.00'), ('Alan Turing', '12.75')) AS t(customer, total)), template := getvariable('tpl'));
Try these queries
Discover the bundled sample_invoice template and render a filled preview from its path in the same query, alongside the placeholder fields it expects.
SELECT t.name, t.fields,
octet_length(docgen.main.docgen_render(t.path, {customer: 'Ada Lovelace', total: '99.50'})) > 0 AS rendered
FROM docgen.main.sample_templates AS t
WHERE t.name = 'sample_invoice'
ORDER BY t.name
Non-DOCX inline bytes degrade to a clean NULL rather than crashing the worker.
SELECT docgen.main.docgen_render('not a docx'::BLOB, {customer: 'Ada'}) AS doc
Uses docgen_render
Request PDF output (pdf=true) from the bundled sample template via headless LibreOffice; NULL when LibreOffice is absent.
SELECT docgen.main.docgen_render((SELECT path FROM docgen.main.sample_templates WHERE name = 'sample_invoice'), {customer: 'Ada', total: '99.50'}, true) AS doc
Merge a whole relation into one document; an empty input relation yields zero rows.
SELECT doc
FROM docgen.main.docgen_merge((SELECT 'Ada' AS customer WHERE false), template := 'invoice.docx')
Uses docgen_merge
List every bundled template with its path and the placeholder fields it expects.
SELECT name, path, fields FROM docgen.main.sample_templates ORDER BY name
Uses sample_templates
Count how many document templates the worker ships out of the box.
SELECT count(*) AS template_count FROM docgen.main.sample_templates
Uses sample_templates
7 more examples in the Catalog, each listed under the table or function it uses.
Docgen is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
Documents & media
Pull tables, words with their positions, page geometry, form fields and metadata out of PDFs, and render pages to PNG
Apache Tika
Documents & media
Pull text and metadata out of PDFs, Office files and other documents, detect their type and language, and OCR scanned images
Image
Documents & media
Decode images, read EXIF and GPS tags, spot near-duplicates by perceptual hash, and make thumbnails