Web optimised PDF

From Scribus Wiki
Jump to navigation Jump to search

Scribus' PDF is optimized for print shops and Scribus takes pains to ensure that the printed output will look identical on different printing presses. The price for this consistency is a file size several times larger than straight-forward PDF would have. This page shows various ways to reduce the file size so you can put the PDF file on the web or even distribute it by e-mail.

See also

First tip

My wife edits and lays out a newsletter that, being mostly distributed via e-mail and the web, must be below ~ 1 MB, desirably even smaller. Since the Scribus options for minimizing the size (subsetting fonts and downsampling images) did not meet that requirement, I looked for a route to PostScript and back to PDF that would give better compression, and the following does a surprisingly good job:

  1. Export as PDF (1.3 or 1.4), embedding all fonts, no font subsetting, no image subsampling → newsletter_scribus.pdf [~2.8 MB]
  2. Convert to PS using `pdftops -level3' → newsletter.ps [huge]
  3. Convert back to PDF using ghostscript (subsetting fonts, subsampling images) → newsletter_compact.pdf [~500 kB]. (You can use ps2pdf13 [or ps2pdf14] for this step; a more fine-grained solution is to use my script below.)
  4. If you feel like it, use pdfopt to linearize the PDF, so Acroread can start showing the first pages while the rest is still being downloaded (I have never tried this feature).

Notes

  • As you see, the resulting PDF is more than 5 times smaller.
  • For step 2, Acroread's print-to-file PS export will not work ― you need to use Xpdf/pdftops.
  • For step 3, you need a 8.x version of ghostscript; I have successfully tried AFPL 8.15 and 8.53 and GNU 8.16, while ESP 7.07 does not work.
  • Marking and searching of text works fine in the new file, while there were many gaps between letters in the original (due to the way Scribus ensures precise text placement).
  • I also found that the compressed PDF file looks much nicer in Acroread 7 (but not in Xpdf or Gv), but your mileage may vary:
    • True-type fonts were not antialiased in the original PDF, but were in the compressed one. However, in simple test documents, antialiasing works for both PDF files, so I don't know what the problem was in the first place.
    • Images looked too dark in the original (apparently a transparency bug in Acroread 6 and 7), but are fine in the compressed file. Again, I have some difficulties in reproducing this in a simple test document.
  • There is one downside: As a result of the transformation, in Xpdf non-ascii letters (like `é' or `½') get lost. Acroread 7 or Gv have no such problem, so it might be a bug in Xpdf/Poppler. For our newsletter, the number of readers using Xpdf is close to one (myself), so this is not much of an issue.
  • Also, the transformation loses meta information (creation date, creator, ...), bookmarks, PDF annotations, hyperlinks, etc. The script below fills in some of the meta information itself; I have tried to extract and restore bookmarks, but the resulting PDFs caused trouble with Acroread 7.


Perl Script

The following Perl script calls, in sequence

  1. pdftk to extract some meta information
  2. pdftops
  3. gs
  4. pdfopt
  • If you lack the first or last program, you'll just have to comment out the corresponding lines.
  • As of 2013, pdfopt is no longer distributed along with ghostcript. See http://bugs.ghostscript.com/show_bug.cgi?id=694099. This page explains that pdfopt doesnt contribute to compress the file anyway... You can either skip this step or get an old version of pdfopt if you insist on using it.
    • For example, as stated in linked page, replace:
my ($pdfopt, @pdfoptargs ) = ('pdfopt' ); line with:
my ($pdfopt, @pdfoptargs ) = ('cp' ); (will be equivalent to "skip this step") or replace it with:
my ($pdfopt, @pdfoptargs ) = ('ps2pdf' ); (will call os2pdf instead of pdfopt...)

Note:

  • Before using it in production, fill in your details in the lines marked with [Insert ... here].
  • You get a usage overview by entering the -h flag on the command line like so:
compress-newsletter.pl -h
  • The script is not very polished, but it works for me.

compress-newsletter.pl

#!/bin/sh
#  -*-Perl-*-
# ====================================================================== #
# Run the right perl version:
if [ -x /usr/local/bin/perl ]; then
  perl=/usr/local/bin/perl
elif [ -x /usr/bin/perl ]; then
  perl=/usr/bin/perl
else
  perl=`which perl| sed 's/.*aliased to *//'`
fi

exec $perl -x -S $0 "$@"     # -x: start from the following line
# ====================================================================== #
#! /Good_Path/perl -w
# line 17

# Name:   compress-newsletter
# Author: wd (Wolfgang.Dobler@ucalgary.ca)
# Date:   03-Oct-2005
# Description:
#   Use ghostscript's pdfwrite device (à la ps2pdf) to reduce the
#   Newsletter's PDF file size, and add meta information like author,
#   date, etc.
#   The preferred route is currently:
#                 [scribus>=1.2.3]
#                        |
#                    file.pdf
#                        |
#                 [pdftops>=3.00]
#                        |
#                     file.ps
#                        |
#            [pstopdf14 (gs-gnu-8.16 or higher)]
#                        |
#                        V
#                    final.pdf
# Usage:
#   compress-newletter [-i col:gray:mono] Newsletter_big.pdf
# Options:
#   -i col:gray:mono
#   --imgres=col:gray:mono   Set resolution for downsampling color,
#                            grayscale and black-and-white images
#                            (default is 144:300:300)
#   --debug                  Be verbose and keep temporary files around
use strict;
use File::Temp qw/ :mktemp /;

use Getopt::Long;
# Allow for `-Plp' as equivalent to `-P lp' etc:
Getopt::Long::config("bundling");

my (%opts);			# Options hash for GetOptions
my $doll='\$';			# Need this to trick CVS

## Process command line
GetOptions(\%opts,
	   qw( -h   --help
	       -i=s --imgres=s
	            --debug
	       -q   --quiet
               -v   --version ));

my $debug = ($opts{'debug'} ? 1 : 0 ); # undocumented debug option
if ($debug) {
    printopts(\%opts);
    print "\@ARGV = `@ARGV'\n";
}

if ($opts{'h'} || $opts{'help'})    { die usage();   }
if ($opts{'v'} || $opts{'version'}) { die version(); }

my $quiet  = ($opts{'q'} || $opts{'quiet'}  || ''           );
my $imgres = ($opts{'i'} || $opts{'imgres'} || '144:300:300');

my ($gs,      @gsargs     ) = ('gs'     );
my ($pdftops, @pdftopsargs) = ('pdftops');
my ($pdfopt,  @pdfoptargs ) = ('pdfopt' );

my $infile = shift or die usage();
(my $root=$infile) =~ s/\.(pdf|ps).*//;
(my $outfile=$infile) =~ s/(.*)(\.(pdf|ps))/${1}_new${2}/;
my $tmpfile = mktemp("${root}.tmp_XXXXXX");


## 0. Extract all sorts of information

# Extract Scribus version, creation date, bookmarks from original PDF:
print "Running pdftk ...\n";
print STDERR "pdftk $infile dump_data output\n" if ($debug);
my $meta = `pdftk $infile dump_data output -`;
my ($creator) = ( $meta =~
		  m{InfoKey: Creator\s+InfoValue:\s*(.+)$}m
		);
$creator = 'Scribus 1.4.3' unless defined($creator);
my $datestring = extract_CreationDate($meta);
my @bookmarks = extract_bookmarks($meta);

# Extract desired image resolutions
my ($colres,$grayres,$monores) = ($imgres =~ /([0-9]+):([0-9]+):([0-9]+)/);
die "Image resolution must be of form `col:gray:mono'\n"
    unless defined($monores);

## 1. Run pdftops
push @pdftopsargs, "-level3";
my $psfile = mktemp("${root}.ps_XXXXXX");
push @pdftopsargs, $infile, $psfile;
print "Running pdftops ...\n";
print STDERR "$pdftops @pdftopsargs\n" if ($debug);
system($pdftops,@pdftopsargs);

## 2. Run gs
# a) Prepare options
push @gsargs, qw{-q -dNOPAUSE -dBATCH};
push @gsargs, '-sDEVICE=pdfwrite';
push @gsargs, '-dCompatibilityLevel=1.3';
# One of /printer, /screen, /prepress, /ebook, /default; see Ps2pdf.htm:
push @gsargs, '-dPDFSETTINGS=/screen';
push @gsargs, '-dEmbedAllFonts=true';
push @gsargs, '-dSubsetFonts=true';
push @gsargs, '-dColorImageDownsampleType=/Bicubic';
push @gsargs, "-dColorImageResolution=$colres";
push @gsargs, '-dGrayImageDownsampleType=/Bicubic';
push @gsargs, "-dGrayImageResolution=$grayres";
push @gsargs, '-dMonoImageDownsampleType=/Bicubic';
push @gsargs, "-dMonoImageResolution=$monores";
push @gsargs, "-sOutputFile=$tmpfile";
push @gsargs, "-c .setpdfwrite";

# b) Write meta information to temporary file
#my $metafile = mktemp("metainfo.tmp_XXXXXX");
my $metafile = "${root}.meta";
open(META, "> $metafile");
print META <<"DEAD_PARROT";
% Document information
[%
 /CreationDate (D:$datestring)
 /ModDate (D:$datestring)
 /Creator ($creator)
 /Title ([Insert your document title here])
 /Subject ([Insert the Subject here])
 /Keywords ([Insert key words here])
 /Author ([Insert author's name here])
 /DOCINFO pdfmark

% Initial view on opening the document
[/View [/Fit] % Fit page in window
 /Page 1
 % /PageMode /UseOutlines % /UseNone /UserOutlines /UseThumbs /FullScreen
 /DOCVIEW pdfmark

DEAD_PARROT

## Bookmarks. [Commented out for acroread 7.0 has problems] Currently at
## the mercy of the original bookmarks (and Scribus 1.2.2 does not allow
## to edit the bookmark names) and the encoding that pdftk understands
## (most quotation marks get mapped to `?').
## Ideally, one would write out the meta information file with
## `compress-newsletter -m CC.pdf' and use it then with
## `compress-newsletter CC.pdf'.
## % Bookmarks: @bookmarks


push @gsargs, '-f', $psfile, $metafile;
print "Running gs ...\n";
print STDERR "$gs @gsargs\n" if ($debug);
system($gs,@gsargs);

## 3. Run pdfopt
print "Running pdfopt ...\n";
print STDERR "$pdfopt @pdfoptargs $tmpfile $outfile\n" if ($debug);
system($pdfopt,@pdfoptargs,$tmpfile,$outfile);

# Some diagnostics:
system('ls', '-l', $infile, $psfile, $tmpfile, $outfile);

END {
    # Clean up even in case of an error:
    unless ($debug) {
        foreach my $file ($psfile,$tmpfile) {
	    unlink $file if (defined($file) && -f $file);
        }
    }
}


# ---------------------------------------------------------------------- #
sub extract_CreationDate {

    use POSIX qw(strftime);

    my $meta = shift;

    my ($cdate) = ( $meta =~
		    m{InfoKey: CreationDate\s+InfoValue:\s*(.+)$}m
		  );
    # Time string: need to splice in "'" after hours and minutes of time zone
    # definition. To me this looks like the technical documentation was taken
    # too literally and now applications (and Acroread 7) insist on these
    # stupid markers.
    my $datestring;
    if ($cdate =~ /[0-9]{14}/) { # managed to extract CreationDate from $meta
	$datestring = "$cdate-06'00'";
    } else {		         # Creation date unknown -- use current date
	my $tz = strftime "%z", localtime();
	$tz =~ s/([0-9][0-9])([0-9][0-9])/$1'$2'/;
	$datestring = strftime "%Y%m%d%H%M%S$tz", localtime();
    }

    $datestring;
}
# ---------------------------------------------------------------------- #
sub extract_bookmarks {

    my $meta = shift;

    my @bm;

    while ($meta =~ /^BookmarkTitle:      \s* (.*) \n
                      BookmarkLevel:      \s* (.*) \n
                      BookmarkPageNumber: \s* (.*) /xmg) {
	my ($title,$level,$page) = ($1,$2,$3);
	push @bm, "[/Title ($title /Page $page /OUT pdfmark\n";
    }

}
# ---------------------------------------------------------------------- #
sub printopts {
# Print command line options
    my $optsref = shift;
    my %opts = %$optsref;
    foreach my $opt (keys(%opts)) {
	print STDERR "\$opts{$opt} = `$opts{$opt}'\n";
    }
}
# ---------------------------------------------------------------------- #
sub usage {
# Extract description and usage information from this file's header.
    my $thisfile = __FILE__;
    local $/ = '';              # Read paragraphs
    open(FILE, "<$thisfile") or die "Cannot open $thisfile\n";
    while (<FILE>) {
	# Paragraph _must_ contain `Description:' or `Usage:'
        next unless /^\s*\#\s*(Description|Usage):/m;
        # Drop `Author:', etc. (anything before `Description:' or `Usage:')
        s/.*?\n(\s*\#\s*(Description|Usage):\s*\n.*)/$1/s;
        # Don't print comment sign:
        s/^\s*# ?//mg;
        last;                        # ignore body
    }
    $_ or "<No usage information found>\n";
}
# ---------------------------------------------------------------------- #
sub version {
# Return CVS data and version info.
    my $doll='\$';		# Need this to trick CVS
    my $cmdname = (split('/', $0))[-1];
    my $rev = '$Revision: 1.8 $';
    my $date = '$Date: 2006/02/02 09:38:52 $';
    $rev =~ s/${doll}Revision:\s*(\S+).*/$1/;
    $date =~ s/${doll}Date:\s*(\S+).*/$1/;
    "$cmdname version $rev ($date)\n";
}
# ---------------------------------------------------------------------- #

# End of file compress-newsletter

Alternative solutions

Using Ghostcript or Python, pdftops and ps2pdf14

In the script collection you can find a python script to Reduce_the_size_of_Scribus_generated_PDFs


A simple 1 line script

  • create a postscript file (directly from Scribus, or by printing the pdf to a file, or by using the "pdftops -level3" command on your PDF file).
  • this one is for letter size PDF only so you ought to edit the option to fit your needs.
  • run the following script:
# make USLetter-sized PDF files using ps2pdf
# USAGE: mkpdf "<filename>.ps"
ps2pdf -dEmbedAllFonts=true -dUseFlateCompression=true -sPAPERSIZE=letter $1 

Or without any script, if your file is myscribuspdf.pdf, just open a terminal and call :

ps2pdf -dEmbedAllFonts=true -dUseFlateCompression=true myscribuspdf.pdf

Trials and results

First tested file : 128 A5 pages, black and white text and drawings, 36.9 Mo

  • Original file "pppOrg.pdf" was produced by the concatenation of 10 Scribus produced files (using the pdftk cat command).
  • Directly through ghostscript command : 20,6 Mo (and many warnings)
gs -dPDFSETTINGS=/prepress -dSAFER -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr \ 
-dGrayImageResolution=300 -dMonoImageResolution=300 -dColorImageResolution=300 -sOutputFile=PPP_gs_command.pdf \
-c .setpdfwrite -f pppOrg.pdf
  • Converted to PS using pdftops -level3 (size : 327.2 Mo; Name : ppp.ps) and then fed to ghostcript : 18Mo (and no warnings)
gs -dPDFSETTINGS=/prepress -dSAFER -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -dGrayImageResolution=300 \
-dMonoImageResolution=300 -dColorImageResolution=300 -sOutputFile=PPPps_gs_command.pdf -c .setpdfwrite -f ppp.ps
  • Using perl script : 7.9 Mo : WIN !
    pdftk detects lots of mysterious repeated errors :
"Error: Could not parse ligature component "cyrillic" of "cyrillic_otmark" in parseCharName" and
"Error: Could not parse ligature component "otmark" of "cyrillic_otmark" in parseCharName"
  • gs runs ok
    pdfopt detects errors such as : "Considering object with an invalid number 1006 as null."
    produced pdf seems to look ok

2nd tested file : 30 A5 color pages, some photos, 15.7 Mo

  • Original file produced with scribus 1.5svn : PA.pdf
  • Directly through ghostscript command : 21 Mo : bigger !
  • Using perl script : 2.77 Mo (and some errors displayed : "Syntax Error: No current point in closepath")

3rd tested file : one page color cover, some photos, 34Mo

pdfopt is not available so i replace it with "cp" as explaned above.

  • Original file produced with scribus 1.4.3 : PPP.pdf, 34Mo
  • using perl script (without pdfopt) : 1,1 Mo - WIN !