kyk's blog

Js Remote Jpeg to Pdf

installation

  1. sharp
  2. image-to-pdf
  3. image-size
import sharp from 'sharp'
import imgToPDF from "image-to-pdf"
import imageSize from "image-size"

const imagePath = './1.jpg'
const pdfPath = './1.pdf'

const imageBuffer = await (await request.get(url)).body()
const jpgBuffer = await sharp(imageBuffer).jpeg({ mozjpeg: true }).toBuffer()
fs.writeFileSync(imagePath, jpgBuffer)

const size = imageSize(imagePath)
const images = [imagePath]
imgToPDF(images, [size.width, size.height]).pipe(
  fs.createWriteStream(pdfPath)
);