gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.21k stars 69 forks source link

Rotating .pdf files via hexapdf #269

Closed rubyFeedback closed 10 months ago

rubyFeedback commented 10 months ago

Sers Thomas,

I am working through a large set of .pdf files from an elderly relative right now, correcting things as I go. That relative has some individual .pdf files, so I merged them. Then I looked at the merged new .pdf file and I noticed that some of the pages were flipped 180° and some 90°. Naturally this is bad, so I decided to first correct it.

Ok, I am also documenting this journey for myself (and future me) and put that discovery into my gem called pdf_paradise (but first I need to finish researching how this is done).

I tried to first go with the commandline, without googling.

So I did:

hexapdf modify --help

That listing has this aptly named intro:

 This command modifies a PDF file. It can be used, for example, to select
 pages that should appear in the output file and/or rotate them. The output
 file can also be encrypted/decrypted and optimized in various ways.

It states "rotate them", so I believe I found the correct subcommand. \o/

That was quick.

However had, now the first problem starts ... if you look at the output, I think it is not instantly clear where or which command to use.

Thus, I have a first suggestion to make - please do feel free to ignore / change in any way as you see fit. It's merely meant as a suggestion from a (commandline) user point of view.

1) I believe it may be useful if hexapdf could get:

hexapdf rotate
hexapdf rotate foobar.pdf # defaulting to 90° clock-work direction

If you think this does not fit there, perhaps you could add a sub-option towards modify, called --rotate.

Then it would be like this:

hexapdf modify rotate foobar.pdf

If a user wants something else then perhaps rotate can get a = parameter such as:

 rotate=43 # this will be 43°

And if you want to go fancy, perhaps:

rotate=-20 # for -20°, this should be counter clock wise then

IF you think this does not fit there, then perhaps the help page of modify could also redirect us to the specific command that allows us to rotate a .pdf file.

Anyway. After I failed to find out how to do so via the commandline + --help, I decided to google.

There I found a few github issue discussions and StackOverflow.

For instance one showed how to do so via pdftk:

 pdftk input.pdf cat 1-endeast output output.pdf
 pdftk input.pdf cat 1-endwest output output.pdf

This is for clockwise and counter-clockwise. Not sure if you want to support the names too;I don't mind the specific command, but it would be neat if it is clearly documented, ideally via examples too (I have not checked the homepage of hexapdf yet, so my comment here may be obsolete and not applicable, so please keep that in mind).

Some other SO solutions are:

pdf90 input.pdf
pdf180 input.pdf
pdf270 input.pdf

That one is interesting because here we specify the rotation in ° already via the name of the binary. Anyway.

pdfjam uses an --angle option:

pdfjam --landscape --angle 90 input.pdf

qpdf, which is also a great toolkit, offers this:

qpdf --rotate=+90 in.pdf out.pdf

Not sure if that fits to hexapdf's commandline parsing model or not, but I think this would also make the most sense for hexapdf. No idea if --rotate can be supported directly as-is via hexapdf, so I just mention that anyway.

ImageMagick convert uses:

convert -rotate 90

Anyway. I think I'll try for qpdf and convert next and see whether that suffices.

I'll also go to the homepage of hexapdf to make sure I can find a rotate example: :)

https://hexapdf.gettalong.org/examples/index.html

Hmm. Can not find one with "rotate" there. Perhaps it could be added as a new entry on the left side, if you have time and think it may be a useful addition.

I think visited:

https://hexapdf.gettalong.org/documentation/hexapdf.1.html#examples

and there are actually examples for rotation, so you cover that already.

(At the least for me it was easier to look at the left side, but I am glad I also visited the examples you provide for commandline applications.)

hexapdf modify input.pdf -i 1r,2-ed output.pdf

Hmm. I think this is fine but I think --rotate may still be easier if one is to work only on a single .pdf file. I did not choose the format of the .pdf files that I received from that elderly person and at the least for me, mentally, it is easier to finish one .pdf file at a page, so the chained commandline example you provide is nice, and we can most likely work on a .pdf file that is a single page anyway, but I think --rotate may still be easier for a use case that just has a single .pdf file.

Anyway, this is just a bit of feedback, please feel free to close/handle this in any way as you see fit. The two toolkits I use the most are hexapdf and qpdf; prawn I am trying to "phase out" eventually, so having everything available in a single toolkit is a win-win situation for my use cases. is a

gettalong commented 10 months ago

Hi! Thanks for the feedback!

gettalong commented 10 months ago

FYI: The first two things (link to manual page in CLI help output, link to CLI examples on left side of examples page) are in place. Since the manual page already has more detailed information about rotating pages, including an example, I won't add a --rotate switch for now.