How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Being able to download blocks as a PDF?

4 repliesLast updated: Nov 29, 2025

Wondering if anyone has thought of this. Being able to download blocks as a PDF? My client is asking for this feature, so it would be cool to hear insights if anyone has done something similar.
AI Update

Great 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 General Approach

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:

This keeps everything within the Sanity ecosystem and scales automatically without needing external hosting.

2. Server-Side Generation

If you're running your own backend, you can create an API endpoint that:

3. Example Workflow

// 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' })

Considerations

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.

Show original thread
4 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

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.

Related contributions