Home pdftk extract random pages from script variable
Reply: 1

pdftk extract random pages from script variable

daniel Published in 2017-11-13 22:52:04Z

I have some pdf files about 2000 pages. They are randomly generated. I need to extract some pages that contain some specific patterns, that changes its page number for every pdf. With some steps using pdfToText and AWK, I can get the page numbers and I store some info into a csv file like that:

PatternA ; 1 3 5 7
PatternB ; 1 8 10 22

I have been trying to do a loop to get and process each line from this csv into the cat option from pdftk command, but it aways return error:

$IFS=$(printf '\n\t')
for line in `cat job.csv`
pattern=`echo $line ¦ cut -d ';' -f 1` 
pages=`echo $line ¦ cut -d ';' - f 2` 
pdftk input.pdf cat $pages output $pattern

When echoing pattern and pages variables, everything are ok. But the pdftk command returns error if I try to get pages from $pages variable:

Error: Unexpected text in page range end, here: 
   1 3 5 7
   Acceptable keywords, for example: "even" or "odd".
   To rotate pages, use: "north" "south" "east"
       "west" "left" "right" or "down"
Errors encountered.  No output created.
Done.  Input errors, so no output created.

What I am doing wrong?


daniel Reply to 2017-11-14 13:31:47Z


I guess.. I don't know if it is the best choice, but works:

Instead directly execute the pdftk command:

pdftk input.pdf cat $pages output $pattern

I stored entire command into a new variable and them, ran the eval command:

cmd="pdftk input.pdf cat $pages output $pattern"
eval $cmd

So, it worked like a charm...

If there are more elegant solution, I'll appreciate!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331713 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO