investmentsraka.blogg.se

Pdf toolkit linux
Pdf toolkit linux




pdf toolkit linux
  1. #Pdf toolkit linux how to
  2. #Pdf toolkit linux pdf

The useful TempFile() function from the standard io/ioutil package ensures that the new file's name does not collide with files that already exist in /tmp, so that different processes won't clobber each other's temp files. To do this, editCmd() creates a temporary file in the /tmp directory. After the user has saved the changes and returned, editCmd() reads the file and saves its contents in array format in the args variable, which was passed in as a pointer. Lines 26 to 28 of Listing 4 link the three to the anchor pads of the same name in the exec package.įor the user to be able to modify the call in the editor, the editCmd() function needs to store the pdftk command and its arguments in a file and call the editor with it. Go offers matching system file descriptors in the os package.

pdf toolkit linux

To call an external program such as an instance of the editor vi, with which the user can also interact, you have to tell the exec package to not just pass Stdout and Stderr from the external program to the identically named channels of the current terminal but also wire up standard input Stdin, so that any keystrokes made by the user will actually reach the editor. If everything works as intended, line 32 calls the method out.String() to print the captured command for the user's perusal.Īs an additional goody, the flag package provides a simple help function that tells the user which options the program supports if called by typing pdftki -h ( Listing 3). If an error occurs, line 29 prints it as a log message. exec then runs the command and collects the output in the buffer. Lines 24 and 25 assign out buffers of the Buffer type from the bytes package to the respective attributes. The handy os/exec package from the Go standard library uses Run() to call external programs and their arguments if so desired, it also captures their standard output and standard error output. The user can now modify the arguments determined in line 15 for the PDFtk call in an editor ( Figure 1) before line 22 is executed. In this case, line 18 starts the editCmd() function, which I'll get to later in Listing 4. By default, it is set to false, but it changes to true if the user specifies -e. Īfter calling Parse() in line 14, the edit variable contains a pointer to a bool type value. But what if you need to leave out a page because you have two copies of it, like at the end of book-1.pdf and the start of book-2.pdf? Thanks to PDFtk, you do this by assigning an uppercase letter to each of the documents and letting the cat statement for the second document start at page 2 instead of page 1 ( Listing 1). It's nice, compact, and easy to remember. And all of this is part of just one simple call: $ pdftki book-*.pdf It decides on book.pdf as the name of the target file, as this is the largest common denominator of all the subfiles.

#Pdf toolkit linux pdf

The freshly squeezed Go program you are looking at today, aptly named Pdftki, simply grabs the PDF book parts, discovers that they all start with book-*, and concatenates them. So far, so good, but couldn't this all be a little more standards compliant and easier? Yes, We Can! Finally, PDFtk expects the name of the output file after the keyword. The cat subcommand tells the tool to stitch together all the input documents in sequence. What happens here is that the shell grabs any files matching the pattern ( book-*.pdf) in the current directory, and – if they are numbered book-1.pdf, book-2.pdf, and so on – passes them to PDFtk in the correct order. PDFtk makes putting the parts together a breeze: $ pdftk book-*.pdf cat output book.pdf Sometimes the cover of a hardback book simply will not fit through the scanner feeder, which means that the front and back covers are individual PDF files from a flatbed scanner.

pdf toolkit linux

Sometimes a book comes out as two or more PDFs because the scanner got stuck between two stacks and I had to continue the scanning process with a new document.

#Pdf toolkit linux how to

En route to doing this, the inclined reader will discover how Go reads and writes files, extracts and manipulates single characters from strings, and calls external programs with interactive input, as well as find out how to cross-compile Go programs for various platforms.įor close to a decade now, I've been digitizing paper books and magazines with my scanner and then tossing them into the recycling bin. That's why I decided to wire up a variant in Go, which tries to guess what the user wants. However, when called, the tool expects unusual syntax for passing parameters, which I find hard to memorize and type every time. One of my favorite tools at the command line is the PDFtk utility, a veritable Swiss Army knife for merging PDF documents.






Pdf toolkit linux