Pregunta

I'm using Matlab to output a multi-page PS file:

print(figure, '-dpsc2', fullfile(folder, [file '.ps']), '-r600', '-append')

and then using Matlab to invoke Ghostscript to convert the resulting PS file to a PDF:

system(['"' gsPath '" -sDEVICE=pdfwrite \
 -dDEVICEWIDTHPOINTS=' num2str(int32(width*72)) ' \
 -dDEVICEHEIGHTPOINTS=' num2str(int32(height*72)) ' \
 -dPDFFitPage \
 -o "' fullfile(folder, [file '.pdf']) '" "' fullfile(folder, [file '.ps']) '"']);

which is just a really hard-to-read way of writing something along the lines of

gswin64c -sDEVICE=pdfwrite ^
 -dDEVICEWIDTHPOINTS=100 ^
 -dDEVICEHEIGHTPOINTS=100 ^
 -dPDFFitPage ^
 -o "C:\folder\output.pdf" "C:\folder\input.ps"

where I've put in example values for device dimensions and input/output paths. When I use this code to print a single figure (one page) to PDF, everything works perfectly. However, when printing multiple figures (multiple pages) to PDF, Ghostscript throws an error:

GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Unable to open the initial device, quitting.

Now, if I remove the -dDEVICEWIDTHPOINTS=100 -dDEVICEHEIGHTPOINTS=100 part of my Ghostscript command and again attempt to print multiple figures to a PDF, it works fine (except for the page size being different than what I want).

GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusSanL-Regu font from %rom%Resource/Font/NimbusSanL-Regu... 4032872 2490784 2311720 1014184 2 done.

Has anyone else run into a similar problem and found a workaround for this issue? One of the keys here is that I need to be able to control the page size of the PDF produced. Thanks!

¿Fue útil?

Solución

Below is an example that should run fine. First we create a multi-page PS file:

fname = 'test';
if exist([fname '.ps'], 'file'), delete([fname '.ps']); end

hfig = figure;
for i=1:10
    plot(cumsum(rand(100,1)-0.5))
    drawnow
    print(hfig, '-dpsc2', '-append', [fname '.ps'])
end
close(hfig)

Next we convert it to PDF using Ghostscript, and properly crop the figures:

gs_path = 'C:\Program Files\gs\gs9.07\bin\gswin64c.exe';
gs_opts = '-dBATCH -dNOPAUSE -q';

% ps2pdf
cmd = sprintf('"%s" %s -sDEVICE=pdfwrite -dPDFFitPage -o %s %s', ...
    gs_path, gs_opts, [fname '.pdf'], [fname '.ps']);
disp(cmd); system(cmd);

% get bbox
cmd = sprintf('"%s" %s -sDEVICE=bbox %s', ...
    gs_path, gs_opts, [fname '.pdf']);
disp(cmd); [~,out] = system(cmd);
out = textscan(out, '%s', 'Delimiter','');
bbox = regexp(out{1}, '^%%BoundingBox: (\d+) (\d+) (\d+) (\d+)','tokens','once');
bbox = str2double(vertcat(bbox{:}));
bbox = [min(bbox(:,1:2)) max(bbox(:,3:4))];

% crop to bounding box
cmd = sprintf(['"%s" %s -o %s -sDEVICE=pdfwrite' ...
    ' -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d -dFIXEDMEDIA' ...
    ' -c "<</PageOffset [-%d -%d]>> setpagedevice" -f %s'], ...
    gs_path, gs_opts, [fname '_cropped.pdf'], ...
    bbox(3)-bbox(1), bbox(4)-bbox(2), bbox(1), bbox(2), [fname '.pdf']);
disp(cmd); system(cmd);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top