Post by Jinsong ZhaoIs it possible to add a logo (png or pdf) to a PDF using only
Ghostscript?
Chapman Flack wrote various PostScript libraries, including to allow
including drawing various types of images. See www.anastigmatix.net.
Use it by converting PDF to PS (i.e. pdf2ps), include the Import
resources (plus other prerequisites), translate to where you want the
logo to appear (I generate dynamic output, so use currentpoint
translate), read the file using StatFOO and ReadyFOO, scale as
needed,ExecFOO, then convert back to PDF.
For example, you want something like this at the point you want to
display a JPEG logo:
gsave
/net.anastigmatix.Import /ProcSet findresource begin
10 700 translate % top-left of page
(logo.jpeg)(r)file StatJFIF pop ReadyJFIF
% scale to target size (e.g. 200pt)
% need to divide target size by larger of wx & wy
2 copy gt not {exch} if pop
200 exch div dup scale %
ExecJPEG
end
grestore
(I find that some JPEG files cause errors, so reprocess them with
ImageMagick; convert source.jpeg fixed.jpeg)
This converts source.pdf into output.pdf, displaying logo.jpg on every page:
{
cat anastigmatix.ps # contains required resources
echo "/logo{gsave/net.anastigmatix.Import/ProcSet findresource begin
30 700 translate
(logo.jpg)(r)file StatJFIF pop ReadyJFIF
2 copy gt not{exch}if pop 200 exch div dup scale
ExecJPEG end grestore}def"
pdf2ps - - | sed 's/showpage/logo &/'
} < source.pdf | ps2pdf -dNOSAFER - output.pdf
Note use of -dNOSAFER to allow reading the logo file.