Skip to content

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
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
Result
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.

setup.sql
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;

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'
);

3. Run a query

query.sql
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
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
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'
);

Overview

Mail-merge SQL data into DOCX templates -> filled documents (DOCX/PDF) as BLOBs.

Catalog

Everything docgen exposes once attached — read straight from the worker's released build.

docgen 1 schema
main 1 table · 2 functions

Merge SQL data into DOCX templates -> filled documents (DOCX/PDF) as BLOBs

Schema examples (5)
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;
Result
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');
Tables
sample_templates 3 examples5 cols

The DOCX templates the worker ships, with their path and template variables (discovery table).

Columns
name VARCHAR
Logical template name.
pathnot null VARCHAR
Absolute filesystem path to the .docx template (pass as the `template` argument).
field_countnot null INTEGER
Number of Jinja2 template variables the template expects.
fieldsnot null VARCHAR
Comma-separated Jinja2 template variable names (the keys the STRUCT data must carry).
descriptionnot null VARCHAR
What the template is for.
ConstraintsPRIMARY KEY (name)
Examples (3)

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;
Link
Scalar functions
docgen_render (template VARCHAR, data ANY, pdf BOOLEAN) +3 overloads 8 examples

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.

Arguments
template VARCHAR
Filesystem path to a .docx template.
data ANY
STRUCT of row data; fields become template variables.
pdf BOOLEAN
Convert to PDF via LibreOffice (default DOCX).
Overloads
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
ReturnsBLOB
Examples (8)

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;
Result
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;
Link
Table in-out functions
docgen_merge (data TABLE, pdf := BOOLEAN, template := VARCHAR) 2 examples

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.

Positional arguments
data TABLE
Relation; every column is a template variable.
Named arguments
pdf := BOOLEAN
Convert the merged document to PDF via LibreOffice. · default false
template := VARCHAR
Path to a .docx template (under $VGI_DOCGEN_TEMPLATES). · default ""
Returns columns
doc BLOB
The single merged document -- DOCX by default, or PDF when pdf:=true -- containing one template render per input row, separated by a page break.
Examples (2)
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'));
Link

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

Uses docgen_rendersample_templates

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

Uses docgen_rendersample_templates

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.