Outils pour utilisateurs

Outils du site


Panneau latéral

Tips

Divers

Projets

Ham Radio

Machines

Research

Privé

Études

tips:pdf

PDF manipulation

Fill PDF form with an FDF file

This can be done using pdftk.

$ <in>pdftk form.pdf dump_data_fields > form_fields.txt</in>
$ <in>vim form_fields.txt</in>
[...]
$ # XXX generate data.fdf from form_fields.txt; see http://www.myown1.com/linux/pdf_formfill.shtml
$ <in>pdftk form.pdf fill_form data.fdf output data.pdf</in>

Crop a PDF file

Using PoDoFo

As adapted from here. PoDoFo.

$ <in>podofopdfinfo P in.pdf  | grep MediaBox</in>
        MediaBox: [ 0.000000 0.000000 841.680000 595.080000 ]
$ <in>podofobox in.pdf cropped.pdf crop 42050.0000 000.0000 84168.0000 59508.0000</in> # Note: dimensions are in centi-pts; and the coordinates start from the bottom-right

Using GhostScript

$ <in>gs -o cropped.pdf -sDEVICE=pdfwrite -c "[/CropBox [0 0 280.59 419.52] /PAGES pdfmark" -f input.pdf</in>

The unit of 'CropBox' (and 'MediaBox') are expressed in an unclear (to me) unit. In this example, 280.59 was 99mm.

Using Ghostscript unfortunately scrapes the metadata.

Copy PDF metadata

Once again, PDFTK comes to the rescue.

$ <in>pdftk input.pdf dump_data  | pdftk cropped.pdf update_info - output cropped2.pdf</in>

Split multi-page-on-one-page scans

For example, 2xA5 on one A4 page. As seen here, this requires a Perl script.

split_pdf_A4_to_A5.pl
#!/usr/bin/env perl
# Original found at [0]
# [0] http://fitzcarraldoblog.wordpress.com/2013/11/09/split-an-a4-pdf-file-into-two-a5-pdf-files/
use strict; use warnings;
use PDF::API2;
 
my $filename = shift || 'test.pdf';
my $oldpdf = PDF::API2->open($filename);
my $newpdf = PDF::API2->new;
 
for my $page_nb (1..$oldpdf->pages) {
	my ($page, @cropdata);
 
	$page = $newpdf->importpage($oldpdf, $page_nb);
	@cropdata = $page->get_mediabox;
	$cropdata[2] /= 2;
	$page->cropbox(@cropdata);
	$page->trimbox(@cropdata);
	$page->mediabox(@cropdata);
 
	$page = $newpdf->importpage($oldpdf, $page_nb);
	@cropdata = $page->get_mediabox;
	$cropdata[0] = $cropdata[2] / 2;
	$page->cropbox(@cropdata);
	$page->trimbox(@cropdata);
	$page->mediabox(@cropdata);
}
 
(my $newfilename = $filename) =~ s/(.*)\.(\w+)$/$1.split.$2/;
$newpdf->saveas($newfilename);
 
__END__

This script is trivial to use.

$ <in>./split_pdf_A4_to_A5.pl in.pdf</in>

It will create a 'in.split.pdf' file.

Create a Booklet

Based on this.

$ <in>pdf2ps in.pdf a.ps</in>
$ <in>psbook a.ps b.ps</in>
$ <in>ps2pdf b.ps c.pdf</in>
$ <in>pdfnup c.pdf --nup 2x1</in>

The output is 'c-nup.pdf'.

Insert a Blank Page

As seen here.

This is trivial with pdftk and a blank page of the right size.

$ <in>pdftk A=in.pdf B=blank.pdf cat A1 B A2-end output out.pdf</in>

Create a Blank Page

$ <in>echo "" | ps2pdf -sPAPERSIZE=a5 - blank.pdf</in>

Custom PAPERSIZE dimensions can hakishly be added to /usr/share/ghostscript/${GS_VER}/Resource/Init/gs_statd.ps

tips/pdf.txt · Dernière modification: 2016-07-21 03:25 par shtrom