Skip to content

Render multi-page PDF into PNG concurrently does not meet expectations  #25

@blingboot

Description

@blingboot

I want to render a multi-page PDF into a PNG image concurrently, but the output image is always the content of the same page PDF, which does not meet expectations.
Environment: nodejs, pdf files take your demo/node/sample.pdf as an example
Mock code:

import { PDFiumLibrary } from '@hyzyla/pdfium/dist/index.cjs';

let library, document;    
try {
  library = await PDFiumLibrary.init();
  document = await library.loadDocument(buffer);

  await Promise.all(
    Array.from(document.pages()).map(async page => {
      const image = await page.render({
        scale,
        render,
      });
      if (image?.data) {
        require('fs').writeFileSync(`./test_${page.number}.png`, Buffer.from(image.data));
      }
    })
  );
} catch (error) {
} finally {
  document?.destroy();
  library?.destroy();
}

function render(options: PDFiumPageRenderOptions) {
  return sharp(options.data, {
    raw: {
      width: options.width,
      height: options.height,
      channels: 4,
    },
  })
    .png({
      effort: 1,
      compressionLevel: 9,
    })
    .toBuffer();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions