Count pages in PDF file using Imagemagick - PHP
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...
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, 2022Comments
-
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 usingimagick
.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;
imagick(identify -format %n testfile.pdf)
identify -density 12 -format "%p" testfile.pdf
identify -format %n testfile.pdf
I don't know how to make use of this stuff..
-
Alfred over 12 yearsthis is not working for me.. :( I set $PDFPath = "test.pdf"; , but not working. What should I do?
-
jonjbar over 12 yearsThis thread might help you then: stackoverflow.com/questions/1143841/…
-
Kurt Pfeifle over 11 yearsThis will be extremely slow for multi-page PDFs, especially for ones with more complex graphics on the pages...
-
Giuseppe over 9 yearsUseful 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 almost 9 yearsThis 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 almost 6 yearsthe PDFlib library no longer maintained since 2010. But even worst, its commercial and not public domain.