Count pages in PDF file using Imagemagick - PHP

13,260

Solution 1

I solved it using;

exec("identify -format %n $file")

Solution 2

Instead of using "identify -format %n $file"(which can turn out to be extremely slow for complex or for mult-page PDFs) you should rather use the right tool for the job, pdfinfo:

exec("pdfinfo $file | grep Pages: | awk '{print $2}'")

which is faster by several magnitudes...

Share:
13,260
Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on June 21, 2022

Comments

  • Alfred
    Alfred almost 2 years

    I am using PHP 5 with Apache in my Windows Vista PC. I have Imagemagick already installed and configured. I want to count the total number of pages in a pdf file using imagick.

    I fount one solution here, but dont know how to open pdf file as text and count pages.

    somebody give me a clear solution to count pages using imagemagick like

    identify -format %n testfile.pdf

    From googling, I found some workarounds or examples;

    1. imagick(identify -format %n testfile.pdf)
    2. identify -density 12 -format "%p" testfile.pdf
    3. identify -format %n testfile.pdf

    I don't know how to make use of this stuff..

  • Alfred
    Alfred over 12 years
    this is not working for me.. :( I set $PDFPath = "test.pdf"; , but not working. What should I do?
  • jonjbar
    jonjbar over 12 years
    This thread might help you then: stackoverflow.com/questions/1143841/…
  • Kurt Pfeifle
    Kurt Pfeifle over 11 years
    This will be extremely slow for multi-page PDFs, especially for ones with more complex graphics on the pages...
  • Giuseppe
    Giuseppe over 9 years
    Useful pointer, but I had some difficulty figuring out how to get pdfinfo in my systems. Finally: Debian/Ubuntu: sudo aptitude install poppler-utils; OSX: brew install poppler.
  • gorodezkiy
    gorodezkiy almost 9 years
    This solution will work not in all cases. For me this script didn't work for PDF 1.6, but worked for 1.4 and 1.5 versions
  • Henry
    Henry almost 6 years
    the PDFlib library no longer maintained since 2010. But even worst, its commercial and not public domain.