Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renderPageAsImage missing fonts #15

Open
1 task
mwohlan opened this issue Jun 25, 2024 · 1 comment
Open
1 task

renderPageAsImage missing fonts #15

mwohlan opened this issue Jun 25, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@mwohlan
Copy link

mwohlan commented Jun 25, 2024

Describe the feature

Depending on the fonts used for PDFs, some fonts won't render at all when converted to an image.

To fix this issue we can disable the FontFace option and provide a standardFontDataUrl (through getDocument or getDocumentProxy options object) pointing to the standard_fonts folder.

Here is a working fix, since I might not be the only one facing this issue:

import { Buffer } from 'node:buffer'
import { dirname, resolve } from 'pathe'
import { configureUnPDF, getDocumentProxy,, renderPageAsImage } from 'unpdf'
import type { TypedArray } from 'pdfjs-dist/types/src/display/api'

export async function convertPdfToImg(buffer: ArrayBuffer | TypedArray, width) {
  try {
    await configureUnPDF({
      // Use the official PDF.js build
      pdfjs: () => import('pdfjs-dist'),
    })

    const packagePath = dirname(resolve('node_modules/pdfjs-dist/package.json'))

    const pdf = await getDocumentProxy(buffer, {
      isEvalSupported: false,
      useSystemFonts: false,
      disableFontFace: true,
      standardFontDataUrl: `${packagePath}/standard_fonts/`,

    })


    const pagenumber = 1
   
    const result = await renderPageAsImage(pdf, pageNumber, {
        canvas: () => import('canvas'),
        width,
   })

     

    return result
  }
  catch (error) {
    console.error('Error converting PDF to images:', error)
    throw new Error(`Failed to convert PDF to images: ${error.message}`)
  }
}

I don't know if pdfjs-dist has to be installed separately for this to work. Maybe @johannschopplich can consider integrating this fix.

Additional information

  • Would you be willing to help implement this feature?
@mwohlan mwohlan added the enhancement New feature or request label Jun 25, 2024
@johannschopplich
Copy link
Collaborator

Great idea! PR welcome. 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants