I needed to modify a pdf the other day, the first and last pages needed to be removed.
So I did what anyone would do, I made a Perl program to do it for me.
Then I was so happy tweeted about it.
Olivier Mengué asked what module was used for this. "PDF::API2" but just telling what module seems too easy so I will show you.
First a little confession I didn't write the code from scratch in just a minute, I took an existing program I had that combines two pdf files into one.
This program has some issues, the filenames are hard coded and I really don't care for the indexed for loop, but it works great and fills its need well. Here it is for the curious:
It was almost right to do the trimming so I jiggled things around a little and came up with this:
That is slightly better than the original, couldn't resist just a little clean up. But can it be better?
After a little more rework:
It is now more generic, it takes arguments from the command line for file names. The double loop was cleaned up since it will only process a single set of files at a time. The range operator replaces the indexed for loop. Finally the subroutine was inlined it wasn't adding much anymore so I dropped it.
After I posted the tweet Perl came up as being hard to read, I am interested to know what you think. This is just a short program but it seems pretty readable to me. (the last one is anyway) Certainly not all of my code comes out this clean but not all is going to be maintained either. What do you think?
Quick note on Perl modules for working with pdf files: I used "PDF::API2" for this task where I am manipulating full pages, either putting them together or taking pages out. When I am creating pdfs (putting text and lines onto a page) I have been using "PDF::Create" also in the past I have been known to just write out postscript and send it through ps2pdf. Want samples? Just ask and I will post a few.
That was soooo helpful, and clear, it was just what I needed this morning, a couple little tweaks and I was able to chop the first 10 pages off a pdf which was just what I needed.ReplyDelete
Have a great day,