问题
I've found a solution that claims to do one folder, but I have a deep folder hierarchy of sheet music that I'd like to batch convert from png to pdf. What do my solutions look like?
I will run into a further problem down the line, which may complicate things. Maybe I should write a script? (I'm a total n00b fyi)
The "further problem" is that some of my sheet music spans more than one page, so if the script can parse filenames that include "1of2" and "2of2" to be turned into a single pdf, that'd be neat.
What are my options here?
Thank you so much.
回答1:
Updated Answer
As an alternative, the following should be faster (as it does the conversions in parallel) and also able to handle larger numbers of files:
find . -name \*.png -print0 | parallel -0 convert {} {.}.pdf
It uses GNU Parallel which is readily available on Linux/Unix and which can be simply installed on OSX with homebrew
using:
brew install parallel
Original Answer (as accepted)
If you have bash
version 4 or better, you can use extended globbing
to recurse directories and do your job very simply:
First enable extended globbing
with:
shopt -s globstar
Then recursively convert PNGs to PDFs:
mogrify -format pdf **/*.png
回答2:
You can loop over png
files in a folder hierarchy, and process each one as follows:
find /path/to/your/files -name '*.png' |
while read -r f; do
g=$(basename "$f" .png).pdf
your_conversion_program <"$f" >"$g"
done
To merge pdf
-s, you could use pdftk
. You need to find all pdf
files that have a 1of2
and 2of2
in their name, and run pdftk
on those:
find /path/to/your/files -name '*1of2*.pdf' |
while read -r f1; do
f2=${f1/1of2/2of2} # name of second file
([ -f "$f1" ] && [ -f "$f2" ]) || continue # check both exist
g=${f1/1of2//} # name of output file
(! [ -f "$g" ]) || continue # if output exists, skip
pdftk "$f1" "$f2" output "$g"
done
See:
- bash string substitution
回答3:
Regarding a deep folder hierarchy you may use find with -exec option. First you find all the PNGs in every subfolder and convert them to PDF:
find ./ -name \*\.png -exec convert {} {}.pdf \;
You'll get new PDF files with extension ".png.pdf" (image.png would be converted to image.png.pdf for example) To correct extensions you may run find command again but this time with "rename" after -exec option.
find ./ -name \*\.png\.pdf -exec rename s/\.png\.pdf/\.pdf/ {} \;
If you want to delete source PNG files, you may use this command, which deletes all files with ".png" extension recursively in every subfolder:
find ./ -name \*\.png -exec rm {} \;
回答4:
if i understand :
- you want to concatenate all your png files from a deep folders structure into only one single pdf.
so...
- insure you png are ordered as you want in your folders
- be aware you can redirect output of a command (say a search one ;) ) to the input of convert, and tell convert to output in one pdf.
General syntax of convert :
convert 1.png 2.png ... global_png.pdf
The following command :
convert `find . -name '*'.png -print` global_png.pdf
- searches for png files in folders from cur_dir
- redirects the output of the command
find
to the input ofconvert
, this is done by back quotingfind
command - converts works and output to pdf file
(this very simple command line works fine only with unspaced filenames, don't miss quoting the wild char, and back quoting the find
command ;) )
[edit]Care....
be sure of what you are doing.
if you delete your png files, you will just loose your original sources...
- it might be a very bad practice...
- using
convert
without any tricky-quality
output option could create an enormous pdf file... and you might have to re-convert
with-quality "60"
for instance... - so keep your original sources until you do not need them any more
来源:https://stackoverflow.com/questions/38837645/batch-convert-pngs-to-individual-pdfs-while-maintaining-deep-folder-hierarchy-in