
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! Generating PDFs from Portable Text blocks is definitely doable, and several people in the community have tackled this. Here's how you can approach it:
The key is to use Portable Text serializers to transform your blocks into an intermediate format that a PDF library can work with. You have a few options:
The modern approach is to use Sanity Functions - serverless functions that can process your content. You could create a function that:
puppeteer, playwright, or @react-pdf/rendererThis keeps everything within the Sanity ecosystem and scales automatically without needing external hosting.
If you're running your own backend, you can create an API endpoint that:
@portabletext/to-html or custom serializers// Serialize Portable Text to HTML
import { toHTML } from '@portabletext/to-html'
const html = toHTML(blocks, {
components: {
types: {
image: ({value}) => `<img src="${value.asset.url}" />`,
// Add custom handlers for your block types
}
}
})
// Then use puppeteer or similar
import puppeteer from 'puppeteer'
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setContent(html)
const pdf = await page.pdf({ format: 'A4' })The ultimate guide for customizing Portable Text has great examples of custom serializers that would be helpful here.
Would love to hear what approach you end up taking! If you share more about your block structure, the community might have more specific suggestions.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store