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

pdf page count #95

Open
savgiannis opened this issue Mar 12, 2021 · 3 comments
Open

pdf page count #95

savgiannis opened this issue Mar 12, 2021 · 3 comments

Comments

@savgiannis
Copy link

I can specify the page number to be converted or the number -1 to convert all pages. Is there any way to know the page count to setup a loop? Possibly to await each page conversion or convert in batches until there are no other pages.

If not then this is a feature request. If yes could you tell me the trick?

By the way this plugin is awesome. Thank you.

@PaulMest
Copy link

My team uses pdf.js from Mozilla to get the metadata. Here is an example that uses pdf.js to get the total count of pages:

https://github.com/mozilla/pdf.js/blob/master/examples/node/getinfo.js

@redstone78
Copy link

redstone78 commented Jul 8, 2021

I used the pdf-lib package and used the getPageCount to get the # of pages. https://github.com/Hopding/pdf-lib/blob/d213f921237daca7a5e21fb38eb69d733ff34796/src/api/PDFDocument.ts#L539

Example:
const pdfLoadDoc = await PDFDocument.load(Buffer.from(pdfFile.Body, 'base64'))
const pageCount = pdfLoadDoc.getPageCount()
console.log('page count', pageCount)

@irgipaulius
Copy link

irgipaulius commented Feb 4, 2022

import { getDocument } from "pdfjs-dist";

/** returns size and resolution of the pdf */
export async function getPdfFormatInfo(dataBuffer: Buffer): Promise<{
  numPages: number;
  width: number;
  height: number;
}> {
  const pdfDocument = await getDocument({ data: dataBuffer }).promise;
  const page = await pdfDocument.getPage(1);
  const viewport = page.getViewport({ scale: 1 });

  const width = Math.floor(viewport.width);
  const height = Math.floor(viewport.height);
  const finalHeight = 1080;
  const finalWidth = (finalHeight / height) * width;

  return {
    numPages: pdfDocument.numPages,
    width,
    height,
    finalWidth,
    finalHeight,
  };
}

savgiannis here you go.

also, have a look at this example: https://github.com/yakovmeister/pdf2pic-examples/blob/master/from-file-to-images.js

  const convert = fromPath(specimen1, baseOptions);
  
  return convert.bulk(-1);

seems like -1 is already implemented. but I do not recommend it, because for me, imagemagick runs out of memory because bulk instantiates it for each pdf page.

Do you think it's ok to close this issue now?

@KaKi87 KaKi87 mentioned this issue Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants