Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Extract images from PDF file with jspdf or other npm package

Good day. Is there any way to extract image from pdf file using jspdf library or other npm libraries. Maybe there are some solutions for react?

I will be grateful for help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I got all images from pdf file using 'pdf-lib' and 'stream-mime-type' libraries. Here is the function itself, I hope it will be useful.

const {PDFDocument} = require('pdf-lib');
const fs = require('fs')
const {getMimeType} = require('stream-mime-type')
const createJpgFile = require('./utils/createJpgFile.js')
const path = require("path");

async function getImageFromPdf() {
  const existingPdfBytes = await new Promise((resolve, reject) => {
    fs.readFile(path.join(rootPath, 'test.pdf'), (err, result) => {
      if (err) {
        reject(err)
      }
      if (!err) {
        resolve(result)
     }
   })
  })
  const pdfDoc = await PDFDocument.load(existingPdfBytes)
  const pages = pdfDoc.getPages()
   const result = []
  pages[0].doc.context.indirectObjects.forEach(el => {
     if (el.hasOwnProperty('contents')) result.push(el.contents)
   })
  const mime = await Promise.all(result.map(async (el) => {
     return new Promise(async (resolve) => {
       const res = await getMimeType(el)
       if (res) {
         resolve(res)
       }
     })
   }));
   await Promise.all(mime.map(async (el, i) => {
       if (el.mime === 'image/jpeg') {
         return new Promise(async (resolve) => {
           const res = await writeJpgFile(result[i], `image-${i}`, 
 'jpg')
          resolve(res)
         })
       }
     })
   )
 }

add: I also wrote a function that parses jpg images from a pdf file without using third-party libraries. The function finds the beginning of jpg images by signature. For other formats, you need to use other signatures.

const parserJpegFromPdf = async () => {
  const existingPdfBytes = await getArrayBufferFromPdf()
  const convertedBuffer = new Uint8Array(existingPdfBytes)
  const firstBeginSignatureSymbol = parseInt('ff', 16)
  const secondBeginSignatureSymbol = parseInt('d8', 16)
  const indexesStartSignatureImage = []
  convertedBuffer.forEach((el, i) => {
    if (el === firstBeginSignatureSymbol &&
      convertedBuffer[i + 1] === secondBeginSignatureSymbol &&
      convertedBuffer[i + 2] === firstBeginSignatureSymbol &&
      (convertedBuffer[i + 3] === parseInt('e0', 16) ||
      convertedBuffer[i + 3] === parseInt('e1', 16) ||
      convertedBuffer[i + 3] === parseInt('e2', 16) ||
      convertedBuffer[i + 3] === parseInt('e3', 16) ||
      convertedBuffer[i + 3] === parseInt('e8', 16))) {
        indexesStartSignatureImage.push(i)
    }
  })
  const resultSlicedCodeImage = indexesStartSignatureImage.reduce((arr, 
el) => {
    arr.push(convertedBuffer.slice(el));
    return arr
  }, [])
  await Promise.all(resultSlicedCodeImage.map(async (el, i) => {
    await createFile(el, `test_image_${i}`, 'jpeg')
  }))
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda