Bash and multiple pdf

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
newone
Posts: 4
Joined: 2019-05-01T23:15:10-07:00
Authentication code: 1152

Bash and multiple pdf

Post by newone »

Hi,

so here is what I'm trying to do. I have a bunch of PDF files called:

67123_001.pdf
67134_001.pdf

which are multiple page pdfs.

I would like to convert them to single page .tif files and keep the first number of the name before'_' and increase the count after '_' for each page. So for example if "67123_001.pdf" has 3 pages, I would like to get the following result:

"67123_001.tif"
"67123_002.tif"
"67123_003.tif"

I also need a batch to do this, since I have > 40.000 documents.

I already have a solution for one part of the problem: a good resolution when converting pdf to tif and a count for the single pages when I split the pdf file. I still haven't figured out how to write a batch and how to keep the first part of the name. Here's what i git so far:

Code: Select all

convert -density 300 67123_001.pdf -depth 8 -strip -background white -alpha off output_%03d.tif
Thanks for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bash and multiple pdf

Post by fmw42 »

I have moved your post from Consulting, since that forum is for PAID consulting only.

What is your platform -- Linux, Mac OSX or Windows? Scripting is different for Linux/Mac vs Windows.

Doing -background white -alpha off will not make the background white if the PDF has transparency. You would need to doe -background white -alpha background -alpha off.
newone
Posts: 4
Joined: 2019-05-01T23:15:10-07:00
Authentication code: 1152

Re: Bash and multiple pdf

Post by newone »

Thanks! Linux RedHat
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bash and multiple pdf

Post by fmw42 »

Using bash scripting

Code: Select all

for img in /path_to_images/*.pdf; do
name=$(convert "$img" -format "%t\n" info: | -head 1)
convert -density 300 "$img" -depth 8 -strip -background white -alpha background -alpha off ${name}_%03d.tif
done
for the line starting with name, see string formats at https://imagemagick.org/script/escape.php

If your PDF is in CMYK, then you would need to add -colorspace sRGB between your -density 300 and the "$img"
newone
Posts: 4
Joined: 2019-05-01T23:15:10-07:00
Authentication code: 1152

Re: Bash and multiple pdf

Post by newone »

Code: Select all

for img in ./*.pdf; do
> name=$(convert "$img" -format "%t\n" info: | -head 1)
> convert -density 300 "$img" -depth 8 -strip -background white -alpha background -alpha off ${name}_%03d.tif
> done
-bash: -head: command not found
newone
Posts: 4
Joined: 2019-05-01T23:15:10-07:00
Authentication code: 1152

Re: Bash and multiple pdf

Post by newone »

Thanks! This is what I get"-bash: -head: command not found" and the result ist "_000.tif"
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bash and multiple pdf

Post by snibgo »

I think "head" shouldn't have a hyphen.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Bash and multiple pdf

Post by fmw42 »

My typo. Snibgo is correct. It should be "head -n 1"

Code: Select all

for img in /path_to_images/*.pdf; do
name=$(convert "$img" -format "%t\n" info: | head -n 1)
convert -density 300 "$img" -depth 8 -strip -background white -alpha background -alpha off ${name}_%03d.tif
done
Post Reply