commit 4867cc4395b38053640de3795d39496b90aa273c
Author: Riaz <riaz@riazj.com>
Date: Fri, 3 Oct 2025 18:41:32 -0700
Initial commit
Diffstat:
A | LICENSE | | | 14 | ++++++++++++++ |
A | Makefile | | | 29 | +++++++++++++++++++++++++++++ |
A | README | | | 28 | ++++++++++++++++++++++++++++ |
A | main.cpp | | | 19 | +++++++++++++++++++ |
4 files changed, 90 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,14 @@
+BSD Zero Clause License
+
+Copyright (C) 2025 by Riaz
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
diff --git a/Makefile b/Makefile
@@ -0,0 +1,29 @@
+CXX = g++
+CXXFLAGS = -std=c++17 -Wall -Wextra -pedantic -O2 -g
+
+PKG_CONFIG = pkg-config
+PKG_CFLAGS = $(shell $(PKG_CONFIG) --cflags poppler-cpp)
+PKG_LIBS = $(shell $(PKG_CONFIG) --libs poppler-cpp)
+
+TARGET = pdfpages
+SRCS = main.cpp
+OBJS = $(SRCS:.cpp=.o)
+PREFIX ?= /usr/local
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS)
+ $(CXX) $(CXXFLAGS) $(OBJS) $(PKG_LIBS) -o $(TARGET)
+
+%.o: %.cpp
+ $(CXX) $(CXXFLAGS) $(PKG_CFLAGS) -c $< -o $@
+
+clean:
+ rm -f $(OBJS) $(TARGET)
+
+install: all
+ mkdir -p $(PREFIX)/bin
+ cp -f $(TARGET) $(PREFIX)/bin
+ chmod 755 $(PREFIX)/bin/$(TARGET)
+
+.PHONY: all clean install
diff --git a/README b/README
@@ -0,0 +1,28 @@
+pdfpages - count the pages of a PDF
+
+This was made by AI. I know neither C++ nor Poppler's API. This is the script
+I use to get a list of PDFs with their page count.
+
+ #!/bin/sh -e
+
+ cd "$HOME/data/books"
+ for f in *.pdf; do
+ pdfpages "$f" >> page-count.txt.tmp
+ done
+
+ sort -V page-count.txt.tmp > page-count.txt
+ rm page-count.txt.tmp
+
+The difference is only a few tenths of a second when using pdfinfo instead of this program.
+
+ #!/bin/sh -e
+
+ cd "$HOME/data/books"
+ for f in *.pdf; do
+ pages="$(pdfinfo "$f" | grep Pages)"
+ pages="${pages##* }"
+ echo "$pages: $f" >> page-count.txt.tmp
+ done
+
+ sort -V page-count.txt.tmp > page-count.txt
+ rm page-count.txt.tmp
diff --git a/main.cpp b/main.cpp
@@ -0,0 +1,19 @@
+#include <poppler-document.h>
+#include <iostream>
+#include <memory>
+
+int main(int argc, char** argv)
+{
+ if (argc < 2) {
+ std::cerr << "usage: " << argv[0] << " pdf\n";
+ return 1;
+ }
+
+ auto doc = std::unique_ptr<poppler::document>(poppler::document::load_from_file(argv[1]));
+ if (!doc) {
+ return 1;
+ }
+
+ std::cout << doc->pages() << ": " << argv[1] << '\n';
+ return 0;
+}