Work with PDF bytes
markdownToPdf returns a Uint8Array containing the complete PDF. This introduction shows you how to write those bytes in Node.js, return them from an HTTP handler, and download them in a browser.
Write a file in Node.js
Pass the returned bytes directly to writeFile. Pardown doesn't choose a filename or write to disk when you use @pardown/core.
import { writeFile } from 'node:fs/promises';
import { markdownToPdf } from '@pardown/core';
const markdown = '# Delivery note\n\nOrder **A-1042** is ready.';
const pdf = await markdownToPdf(markdown);
await writeFile('delivery-note.pdf', pdf);writeFile replaces an existing file at the same path. Create its parent directory before writing when it doesn't already exist.
Return an HTTP response
Set the PDF media type and return the bytes as the response body. Add Content-Disposition when you want the browser to suggest a download filename.
import { markdownToPdf } from '@pardown/core';
export async function createReportResponse(): Promise<Response> {
const markdown = '# Weekly report\n\nAll services are operational.';
const pdf = await markdownToPdf(markdown);
return new Response(pdf, {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename="weekly-report.pdf"',
},
});
}The example uses the standard Response API. Adapt the response construction to your server framework when it uses a different API.
Download a PDF in a browser
Create a PDF Blob, generate a temporary object URL, and activate a download link. Revoke the URL after starting the download.
import { markdownToPdf } from '@pardown/core';
const pdf = await markdownToPdf('# Travel itinerary');
const blob = new Blob([pdf], { type: 'application/pdf' });
const downloadUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = 'travel-itinerary.pdf';
link.click();
URL.revokeObjectURL(downloadUrl);This example doesn't add the link to the document because browsers can activate a detached link. If your supported browser requires the link to be attached, add it before click() and remove it afterwards.
Store or upload the result
Pass the Uint8Array to an API that accepts binary data. If an SDK requires an ArrayBuffer, use the returned array's buffer property.
const pdf = await markdownToPdf('# Archive record');
const pdfBuffer = pdf.buffer;Pardown buffers the complete document before resolving, so the library doesn't provide a streaming PDF response.
Next steps
Continue with the reference that matches your conversion requirements:
- Configure local, remote, or browser images in Image loading and cancellation.
- Collect recoverable warnings in Diagnostics.
- Review exact signatures in the
@pardown/coreAPI reference.