Describe the feature
I propose enhancing the renderPageAsImage function adding a toDataURL option:
- Introduce a new boolean option,
toDataURL, to the options object.
- When
options.toDataURL is set to true, the function's return type should be Promise<string>, resolving with the data URL of the rendered page.
- When the option is
false or not provided, the function should maintain its existing behavior, returning a Promise<ArrayBuffer>.
This can be achieved with TypeScript overloads to ensure type safety:
// Returns a buffer by default
function renderPageAsImage(
data: DocumentInitParameters['data'] | PDFDocumentProxy,
pageNumber: number,
options?: {
// ... other options
toDataURL?: false
},
): Promise<ArrayBuffer>
// Returns a data URL string when toDataURL is true
function renderPageAsImage(
data: DocumentInitParameters['data'] | PDFDocumentProxy,
pageNumber: number,
options: {
// ... other options
toDataURL: true
},
): Promise<string>
Example Usage:
import { getDocumentProxy, renderPageAsImage } from 'unpdf'
const buffer = await readFile('./document.pdf')
const pdf = await getDocumentProxy(new Uint8Array(buffer))
const pageNumber = 1
// Get the image as a data URL for web embedding
const dataUrl = await renderPageAsImage(pdf, pageNumber, {
toDataURL: true,
})
// Example: <img src="data:image/png;base64,iVBORw0KGgo...">
const html = `<img src="${dataUrl}">`
// ...
Additional information
Describe the feature
I propose enhancing the
renderPageAsImagefunction adding atoDataURLoption:toDataURL, to theoptionsobject.options.toDataURLis set totrue, the function's return type should bePromise<string>, resolving with the data URL of the rendered page.falseor not provided, the function should maintain its existing behavior, returning aPromise<ArrayBuffer>.This can be achieved with TypeScript overloads to ensure type safety:
Example Usage:
Additional information