Skip to content

Instantly share code, notes, and snippets.

@karolba
Last active November 26, 2025 11:59
Show Gist options
  • Select an option

  • Save karolba/dedbe8d98955704f56c4524fdde18c92 to your computer and use it in GitHub Desktop.

Select an option

Save karolba/dedbe8d98955704f56c4524fdde18c92 to your computer and use it in GitHub Desktop.
Convert comics (.rar, .zip, .cbz, .cbx) to pdfs based on their internal file structure
# remember to not use the old builtin gnu make on macos
ifeq ($(word 1,$(subst ., ,$(MAKE_VERSION))),3)
_ := $(error use a newer gnu make)
endif
SHELL := bash
manga_dir := $(HOME)/Documents/manga
escape = '$(subst ','\'',$1)'
img2pdf_opts := --rotation=ifvalid --engine=internal
find_image_files = \
find $1 -type f -exec file --mime-type --no-pad --separator / -- '{}' '+' \
| sed -n 's|/ image/[^ ]*$$||p' \
| grep -vF /__MACOSX/ \
| grep -vF /.DS_Store/
define pdfize
mkdir -p $(call escape,$@)/_unpack
cd $(call escape,$@)/_unpack && \
$1 $(call escape,$^)
cd $(call escape,$@) && \
$(call find_image_files,.) \
| sed 's|/[^/]*$$||' \
| sort --version-sort \
| uniq \
| while read -r comic_dir; do \
echo "Processing $$comic_dir"; \
$(call find_image_files,"$$comic_dir" -maxdepth 1) \
| sort --version-sort \
| tr '\n' '\0' \
| img2pdf \
--from-file - \
$(img2pdf_opts) \
-o ./"$$(echo "$$comic_dir" | tr / - | sed 's|^\.-_unpack-||')".pdf -- "$${files[@]}"; \
done
rm -r $(call escape,$@)/_unpack
endef
.PHONY: all
all:
shopt -s nullglob \
&& pushd $(call escape,$(manga_dir)) \
&& all=( \
*.zip */*.zip \
*.rar */*.rar \
*.cbz */*.cbz \
*.cbr */*.cbr \
) \
&& popd \
&& $(MAKE) "$${all[@]/%//}"
%.rar/: $(manga_dir)/%.rar; $(call pdfize,unrar x)
%.cbr/: $(manga_dir)/%.cbr; $(call pdfize,unrar x)
%.zip/: $(manga_dir)/%.zip; $(call pdfize,unar)
%.cbz/: $(manga_dir)/%.cbz; $(call pdfize,unar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment