Can anyone suggest how to extend page size on one side using postscript? I have to put a mark on a heavy bunch of documents. I use postscript for this (as technology that most native to pdfs, obviously as speed is critical for the task).
I am able to put a mark on the document itself, but I have to add a blank field to the right of each page and this is the problem.
This is what is going on (the mark [copy] is added above the pdf)
This is what has to be (page size extended with blank field on the right):
This is the content of my mark.ps file
<<
/EndPage
{
2 eq { pop false }
{
gsave
7.0 setlinewidth
70 780 newpath moveto
70 900 lineto
120 900 lineto
120 780 lineto
66.5 780 lineto
stroke
closepath
/Helvetica findfont 18 scalefont setfont
newpath
100 792.5 moveto
90 rotate
(COPY) true charpath fill
1 setlinewidth stroke
0 setgray
grestore
true
} ifelse
} bind
>> setpagedevice
This is how it is applied to pdf
gs \
-q \
-dBATCH \
-dNOPAUSE \
-sDEVICE=pdfwrite \
-sOutputFile=output.pdf \
-f mark.ps \
input.pdf
What I tried :
Change page size << /PageSize [595 1642] >> setpagedevice - didnt work
use -g option in gs like this: -g595x1642 - also didnt work fine
If someone has relevant suggestions please share!
Related
Hi I'm wondering how I can scale down / resize a pdf content page per page and my pages could have different size and orientation. My expected result would be a A4 page with the content that best fit to it.
Based on these topics:
How to resize content of PDF without affecting page size?
Using GhostScript to get page size
How to make per-page changes to a pdf document using Ghostscript?
I written a simple code but since I'm really begin with this it is probably not the way to do it.
const script = `
FileName (r) file % open file given by -sFileName
runpdfbegin % open file as pdf
1 1 pdfpagecount { % for each page index
pdfgetpage % get pdf page properties (pushes a dict)
/MediaBox get % get MediaBox value from dict (pushes an array of numbers [ offsetX offsetY width height ])
% Define the new expected height & width
/nheight ${newHeight} def
/nwidth ${newWidth} def
% Destruct the array to the stack
aload pop
/pheight exch def
/pwidth exch def
pop pop
% Compute scales
nheight pheight div % compute scale ratio of height
nwidth pwidth div % compute scale ratio of width
% Now we have the two scale ratio
1 index 1 index lt
{ % if scaleY < scaleX
pop
}
{
exch
pop
}
ifelse
/pscale exch def
% At this stage I have the min scale
% Compute translateX
pwidth pscale pwidth % (WIDTH - (scale * WIDTH)) / 2
mul sub 2 div round
/ptranslateX exch def
% Compute translateY
pheight pscale pheight % (HEIGHT - (scale * HEIGHT)) / 2
mul sub 2 div round
/ptranslateY exch def
% Now build the scale and translate operation
<</BeginPage{pscale pscale scale ptranslateX ptranslateY translate}>> setpagedevice
} for`
// Resize the page
await spawnProcess('gs', [
'-sDEVICE=pdfwrite',
'-dPDFSETTINGS=/ebook',
'-dCompatibilityLevel=1.5',
'-dSubsetFonts=true',
'-dEmbedAllFonts=true',
'-dFIXEDMEDIA',
'-dSAFER',
'-dNOPAUSE',
'-dBATCH',
'-dUNROLLFORMS',
'-dAutoRotatePages=/PageByPage',
'-dFitPage',
`-sOutputFile=${filename}-resized.pdf`,
`-dDEVICEWIDTHPOINTS=595`,
`-dDEVICEHEIGHTPOINTS=842`,
// '-c', `<</BeginPage{${scale} ${scale} scale ${translateX} ${translateY} translate}>> setpagedevice`,
'-c', script,
'-sFileName', filename
])
Note: Using ghostscript -dFitPage option works on some pdf but not all of them in contrary to -c "<</BeginPage{${scale} ${scale} scale ${translateX} ${translateY} translate}>> setpagedevice" that works on all the pdfs I tested but does not fit page per page. That's why I'm trying to do a custom script.
It would be great if you could help me to be on the right way
I have SVG documents which I convert to PDF via ghostscript in the end of my workflow. Now I have to add a new feature.
I need to replace some element in the SVG with PDF content. It's not
just overlay, it could be rotated and scaled proportionally, here is an image for example:
"example"
My question is are there any way to do it with ghostscript?
At the moment I just insert it as rasterized image by SVG image elements but I'll need it to be vector for printing.
NOTE: I'll need to take only first page from the inserted PDF.
I know there are a pdftk which can overlay one PDF file over another, but I can't scale and rotate overlayed PDF document with it.
OK so I'm afraid that, due to recent changes in the Ghostscript PDF interpreter to address security vulnerabilities, this code will only work with versions up to 9.26. In future teh PDF itnerpreter will be altered and there will be better ways to achieve this, but I'm afraid that's a long-term goal. For now, to use this code, you'll need to stick with an older version.
The PostScript program which does this is as follows:
%!PS
%%
%% This code is copied from pdf_main.ps, pdfshowpage_finish
%% sadly that routine always calls showpage, and we want that
%% to be under our control, so we have to duplicate the code
%% here. Not only that but it uses GS extensions which aren't
%% available outside of startup, so some things it simply can't
%% replicate. As a result some of the error handling is less
%% good.
%%
%% I plan to extend the PDF interpreter with two new
%% routines, pdfnoshowpage_finish and then have both
%% that and pdfshowpage_finish call pdfoptionalshowpage_finish
%% which will take a boolean determining whether to actually
%% call the showpage. At that time we'll alter this code.
%%
/draw_page_content { % <pagedict> pdfshowpage_finish -
save /PDFSave exch store
/PDFdictstackcount countdictstack store
/PDFexecstackcount count 2 sub store
(before exec) VMDEBUG
% set up color space substitution (this must be inside the page save)
pdfshowpage_setcspacesub
% Display the actual page contents.
8 dict begin
/BXlevel 0 def
/BMClevel 0 def
/OFFlevels 0 dict def
/BGDefault currentblackgeneration def
/UCRDefault currentundercolorremoval def
%****** DOESN'T HANDLE COLOR TRANSFER YET ******
/TRDefault currenttransfer def
matrix currentmatrix
2 dict
dictbeginpage setmatrix
/DefaultQstate qstate store
count 1 sub /pdfemptycount exch store
% If the page uses any transparency features, show it within
% a transparency group.
dup pageusestransparency dup /PDFusingtransparency exch def {
% Show the page within a PDF 1.4 device filter.
0 .pushpdf14devicefilter {
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
% If the page has a Group, enclose contents in transparency group.
% (Adobe Tech Note 5407, sec 9.2)
dup /Group knownoget {
1 index /CropBox pget {
/CropBox exch
} {
1 index get_media_box pop /MediaBox exch
} ifelse
oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup
showpagecontents
.endtransparencygroup
} {
showpagecontents
} ifelse
} stopped {
% abort the transparency device
.abortpdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
stop
} if .poppdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
} {
showpagecontents
} ifelse
.free_page_resources
% todo: mixing drawing ops outside the device filter could cause
% problems, for example with the pnga device.
end % scratch dict
% Some PDF files don't have matching q/Q (gsave/grestore) so we need
% to clean up any left over dicts from the dictstack
PDFdictstackcount //false
{ countdictstack 2 index le { exit } if
currentdict /n known not or
end
} loop
pop
count PDFexecstackcount sub { pop } repeat
Repaired % pass Repaired state around the restore
PDFSave restore
currentglobal pdfdict gcheck .setglobal
.setglobal
/Repaired exch def
} def
% And now, draw the page from teh first PDF file
(d:/temp/SO/target.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store draw_page_content
restore
runpdfend
% and then the page from the secodn PDF file
(d:/temp/SO/source.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store
% Before drawing the second page, adjust the CTM so
% that the bottom left corner of the page is co-incident
% with the bottom left of the area we want to draw the
% page in
75 575 translate
% adjust the size of the output
0.11 0.11 scale
% and rotate it
-46 rotate
draw_page_content
restore
runpdfend
showpage
Save that in a file, lets call it test.ps, and adjust the paths in the PostScript program suitably. Then run Ghostscript with: gs -sDEVICE=pdfwrite -sOutputFile=out.pdf test.ps and you'll get a PDF file something like what I think you want. Obviously you'll also need to change the numbers for translate/scale/rotate.
I'm posting here a solution which support clipping of pasted PDF file in case if somebody will need it in future. I've added additional parameters to draw_page_content to support that. I appeared that "dict beginpage setmatrix" command somehow overrides clipping path and it should be set only after that command. I'm not an expert in PostScript, but at least this solution is working. Thanks again KenS for your help.
%!PS
%%
%% This code is copied from pdf_main.ps, pdfshowpage_finish
%% sadly that routine always calls showpage, and we want that
%% to be under our control, so we have to duplicate the code
%% here. Not only that but it uses GS extensions which aren't
%% available outside of startup, so some things it simply can't
%% replicate. As a result some of the error handling is less
%% good.
%%
%% I plan to extend the PDF interpreter with two new
%% routines, pdfnoshowpage_finish and then have both
%% that and pdfshowpage_finish call pdfoptionalshowpage_finish
%% which will take a boolean determining whether to actually
%% call the showpage. At that time we'll alter this code.
%%
/draw_page_content { % <pagedict> pdfshowpage_finish -
/clipx exch def /clipy exch def /clip_width exch def /clip_height exch def /should_clip exch def
save /PDFSave exch store
/PDFdictstackcount countdictstack store
/PDFexecstackcount count 2 sub store
(before exec) VMDEBUG
% set up color space substitution (this must be inside the page save)
pdfshowpage_setcspacesub
% Display the actual page contents.
8 dict begin
/BXlevel 0 def
/BMClevel 0 def
/OFFlevels 0 dict def
/BGDefault currentblackgeneration def
/UCRDefault currentundercolorremoval def
%****** DOESN'T HANDLE COLOR TRANSFER YET ******
/TRDefault currenttransfer def
matrix currentmatrix
2 dict
dictbeginpage setmatrix
% set clipping here
should_clip 0 gt
{
newpath clipx clipy moveto clipx clipy clip_height add lineto clipx clip_width add clipy clip_height add lineto clipx clip_width add clipy lineto closepath clip
}
{
}
ifelse
/DefaultQstate qstate store
count 1 sub /pdfemptycount exch store
% If the page uses any transparency features, show it within
% a transparency group.
dup pageusestransparency dup /PDFusingtransparency exch def {
% Show the page within a PDF 1.4 device filter.
0 .pushpdf14devicefilter {
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
% If the page has a Group, enclose contents in transparency group.
% (Adobe Tech Note 5407, sec 9.2)
dup /Group knownoget {
1 index /CropBox pget {
/CropBox exch
} {
1 index get_media_box pop /MediaBox exch
} ifelse
oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup
showpagecontents
.endtransparencygroup
} {
showpagecontents
} ifelse
} stopped {
% abort the transparency device
.abortpdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
stop
} if .poppdf14devicefilter
/DefaultQstate qstate store % device has changed -- reset DefaultQstate
} {
showpagecontents
} ifelse
.free_page_resources
% todo: mixing drawing ops outside the device filter could cause
% problems, for example with the pnga device.
end % scratch dict
% Some PDF files don't have matching q/Q (gsave/grestore) so we need
% to clean up any left over dicts from the dictstack
PDFdictstackcount //false
{ countdictstack 2 index le { exit } if
currentdict /n known not or
end
} loop
pop
count PDFexecstackcount sub { pop } repeat
Repaired % pass Repaired state around the restore
PDFSave restore
currentglobal pdfdict gcheck .setglobal
.setglobal
/Repaired exch def
} bind def
% And now, draw the page from teh first PDF file
(/opt/files/main.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store 0 1125 1125 0 0 draw_page_content
restore
runpdfend
gsave
% and then the page from the next PDF file
(/opt/files/placement1.pdf) (r) file runpdfbegin
save
1 pdfgetpage
dup /Page exch store
% Before drawing the second page, adjust the CTM so
% that the bottom left corner of the page is co-incident
% with the bottom left of the area we want to draw the
% page in
120 120 translate
% and rotate it
25 rotate
% adjust the size of the output
0.7 0.7 scale
%let's skew file
[1 0 1.3 1 0 0] concat
% draw file and clip it
1 100 100 0 0 draw_page_content
restore
runpdfend
grestore
showpage
I am creating a PDF file from a TIFF image using ImageMagick and Ghostscript.
My source tiff is 16 bits per channel with no alpha (48 bit image) with an attached ICC Profile (AdobeRGB) and I want to maintaing this in the final PDF.
convert input.tif[0] -density 600 -alpha Off -size 5809x9408 -depth 16 intermediate.ps
This takes my input tiff image (just the main image, and not the thumbnail via using [0]) and creates a .ps file from the bitmap.
When I look at the size of the PostScript file, it's roughly the same size (3-4 MB larger than the 328MB tiff) as the source TIFF, but I can't tell if the image data in the .ps is 8 or 16 bit per channel.
Then, when I use GhostScript to convert this to a PDF, I'm getting 8 bits per channel in the PDF.
gs -dPDFA=1 -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sDefaultRGBProfile=AdobeRGB1998.icc -dOverrideICC -sOutputFile=output.pdf -r600 -P PDFA_def.ps -f custom.joboptions intermediate.ps
If I use pdfimages to inspect the PDF, it shows me 8 bit per channel.
pdfimages -list output.pdf
page num type width height color comp bpc enc interp object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
1 0 image 5809 9408 rgb 3 8 image no 10 0 600 600 74.1M 47%
The contents of my PDFA_def.ps has been modified from the default Ghostscript install to specify AdobeRGB (1998) as the colour profile:
%!
% This is a sample prefix file for creating a PDF/A document.
% Feel free to modify entries marked with "Customize".
% This assumes an ICC profile to reside in the file (ISO Coated sb.icc),
% unless the user modifies the corresponding line below.
% Define entries in the document Info dictionary :
/ICCProfile (AdobeRGB1998.icc) % Customise
def
[ /Title (Title) % Customise
/DOCINFO pdfmark
% Define an ICC profile :
[/_objdef {icc_PDFA} /type /stream /OBJ pdfmark
[{icc_PDFA}
<<
/N currentpagedevice /ProcessColorModel known {
currentpagedevice /ProcessColorModel get dup /DeviceGray eq
{pop 1} {
/DeviceRGB eq
{3}{4} ifelse
} ifelse
} {
(ERROR, unable to determine ProcessColorModel) == flush
} ifelse
>> /PUT pdfmark
[{icc_PDFA} ICCProfile (r) file /PUT pdfmark
% Define the output intent dictionary :
[/_objdef {OutputIntent_PDFA} /type /dict /OBJ pdfmark
[{OutputIntent_PDFA} <<
/Type /OutputIntent % Must be so (the standard requires).
/S /GTS_PDFA1 % Must be so (the standard requires).
/DestOutputProfile {icc_PDFA} % Must be so (see above).
/OutputConditionIdentifier (sRGB) % Customize
>> /PUT pdfmark
[{Catalog} <</OutputIntents [ {OutputIntent_PDFA} ]>> /PUT pdfmark
I've also got a custom.joboptions file that I created in Acrobat Distiller and then have modified for PDF/A compliance - I have tried to force 16-bit images in this file too, but I'm still getting 8-bit images in the PDF.
I don't know how many of these options Ghostscript respects and how many it ignores however. If I don't use this custom.joboptions file when making the PDF, the images are downsampled to a very low resolution.
<<
/ASCII85EncodePages false
/AllowTransparency false
/AutoPositionEPSFiles true
/AutoRotatePages /All
/Binding /Left
/CalGrayProfile (Dot Gain 20%)
/CalRGBProfile (sRGB IEC61966-2.1)
/CalCMYKProfile (U.S. Web Coated \050SWOP\051 v2)
/sRGBProfile (sRGB IEC61966-2.1)
/CannotEmbedFontPolicy /Error
/CompatibilityLevel 1.4
/CompressObjects /Off
/CompressPages true
/ConvertImagesToIndexed true
/PassThroughJPEGImages true
/CreateJobTicket false
/DefaultRenderingIntent /Default
/DetectBlends true
/DetectCurves 0.0000
/ColorConversionStrategy /LeaveColorUnchanged
/DoThumbnails false
/EmbedAllFonts true
/EmbedOpenType false
/ParseICCProfilesInComments true
/EmbedJobOptions false
/DSCReportingLevel 0
/EmitDSCWarnings false
/EndPage -1
/ImageMemory 1048576
/LockDistillerParams true
/MaxSubsetPct 100
/Optimize false
/OPM 1
/ParseDSCComments true
/ParseDSCCommentsForDocInfo true
/PreserveCopyPage true
/PreserveDICMYKValues true
/PreserveEPSInfo true
/PreserveFlatness true
/PreserveHalftoneInfo false
/PreserveOPIComments false
/PreserveOverprintSettings false
/StartPage 1
/SubsetFonts false
/TransferFunctionInfo /Apply
/UCRandBGInfo /Remove
/UsePrologue false
/ColorSettingsFile (None)
/AlwaysEmbed [ true
]
/NeverEmbed [ true
]
/AntiAliasColorImages false
/CropColorImages true
/ColorImageMinResolution 600
/ColorImageMinResolutionPolicy /OK
/DownsampleColorImages false
/ColorImageDownsampleType /Average
/ColorImageResolution 600
/ColorImageDepth -1
/ColorImageMinDownsampleDepth 16
/ColorImageDownsampleThreshold 1.50000
/EncodeColorImages true
/ColorImageFilter /FlateEncode
/AutoFilterColorImages false
/ColorImageAutoFilterStrategy /JPEG
/ColorACSImageDict <<
/QFactor 0.15
/HSamples [1 1 1 1] /VSamples [1 1 1 1]
>>
/ColorImageDict <<
/QFactor 0.15
/HSamples [1 1 1 1] /VSamples [1 1 1 1]
>>
/JPEG2000ColorACSImageDict <<
/TileWidth 256
/TileHeight 256
/Quality 30
>>
/JPEG2000ColorImageDict <<
/TileWidth 256
/TileHeight 256
/Quality 30
>>
/AntiAliasGrayImages false
/CropGrayImages true
/GrayImageMinResolution 300
/GrayImageMinResolutionPolicy /OK
/DownsampleGrayImages false
/GrayImageDownsampleType /Average
/GrayImageResolution 600
/GrayImageDepth -1
/GrayImageMinDownsampleDepth 2
/GrayImageDownsampleThreshold 1.50000
/EncodeGrayImages true
/GrayImageFilter /FlateEncode
/AutoFilterGrayImages false
/GrayImageAutoFilterStrategy /JPEG
/GrayACSImageDict <<
/QFactor 0.15
/HSamples [1 1 1 1] /VSamples [1 1 1 1]
>>
/GrayImageDict <<
/QFactor 0.15
/HSamples [1 1 1 1] /VSamples [1 1 1 1]
>>
/JPEG2000GrayACSImageDict <<
/TileWidth 256
/TileHeight 256
/Quality 30
>>
/JPEG2000GrayImageDict <<
/TileWidth 256
/TileHeight 256
/Quality 30
>>
/AntiAliasMonoImages false
/CropMonoImages true
/MonoImageMinResolution 1200
/MonoImageMinResolutionPolicy /OK
/DownsampleMonoImages false
/MonoImageDownsampleType /Average
/MonoImageResolution 2400
/MonoImageDepth -1
/MonoImageDownsampleThreshold 1.50000
/EncodeMonoImages true
/MonoImageFilter /CCITTFaxEncode
/MonoImageDict <<
/K -1
>>
/AllowPSXObjects false
/CheckCompliance [
/PDFA1B:2005
]
/PDFX1aCheck false
/PDFX3Check false
/PDFXCompliantPDFOnly true
/PDFXNoTrimBoxError false
/PDFXTrimBoxToMediaBoxOffset [
0.00000
0.00000
0.00000
0.00000
]
/PDFXSetBleedBoxToMediaBox true
/PDFXBleedBoxToTrimBoxOffset [
0.00000
0.00000
0.00000
0.00000
]
/PDFXOutputIntentProfile (Adobe RGB \0501998\051)
/PDFXOutputConditionIdentifier ()
/PDFXOutputCondition ()
/PDFXRegistryName ()
/PDFXTrapped /False
/CreateJDFFile false
>> setdistillerparams
<<
/HWResolution [600 600]
/PageSize [697.080 1128.960]
>> setpagedevice
PostScript can't handle 16 bits per component, it only handles 1, 2, 4, 8 and 12.
PDF doesn't support 12 BPC, only 1, 2, 4, 8 and 16.
So there isn't any way to get a PDF file with more than 12 BPC if you use PostScript as an intermediate format. Even if the PDF file says its 16 BPC the actual data will be limited to 12 (16BPC original -> 12 BPC PostScript -> 16 BPC PDF)
Now further to that, you say that you are creating a PDF/A file, and its PDF/A-1. If you read the PDF/A-1 specification you will see that its limited to PDF 1.4, checking the PDF Reference Manual, we find that 16 BPC images were introduced in PDF 1.5
So even if pdfwrite were able to upscale the 12 BPC image to a 16 BPC image (with padding), its not allowed to do so if you want to create a PDF/A-1 file, because that's not allowed by the specification. So I'm afraid you can't do what you want, you can't create a legal PDF/A-1 file with 16 BPC images using any tool.
Regarding downsampling, the default for colour image downsampling is 'false', so if you don't enable it (DownsampleColorImages=true) then the pdfwrite device won't downsample the images.
Good day,
I have large issue cropping the PDF to PNG
PDF is about 1,6MB (2500x2500) and one process takes about 7-10min and generates 700MB of temporary files.e.g.
exec("convert -density 400 'file.pdf' -resize 150% -crop 48x24# png32:'file_%d.png'");
One PDF must generate PNGs from size 25% to 200%
Here i generate attributes like density, size for resizing in % and grids row and column count
$x = 0; $y = 0;
for ($i = 25; $i <= 200; $i += 25) {
$x += 8; $y += 4;
$convert[$i] = ['density' => (($i < 75) ? 200 : ($i < 150) ? 300 : ($i < 200) ? 400 : 500), 'tiles' => implode("x", [$x, $y])];
}
After i launch converter one after one and it's extremely expensive in time.
$file_cropper = function($filename, $additional = '') use ($density, $size, $tiles) {
$pid = exec("convert -density $density ".escapeshellarg($filename)." -resize $size% -crop $tiles# ".$additional." png32:".escapeshellarg(str_replace(".pdf", "_%d.png", $filename))." >/dev/null & echo $!");
do {
/* some really fast code */
} while (file_exists("/proc/{$pid}"));
};
If i launch it simultaneously (8 processes) then ImageMagick eats all the space i have (40GB) => ~35GB of temporary files
Where is my problem, what am i doing wrong?
i tried to pass params below to functions $additional var:
"-page 0x0+0+0"
"+repage"
"-page 0x0+0+0 +repage"
"+repage -page 0x0+0+0"
nothing changes
Version: ImageMagick 6.7.7-10 2016-06-01 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Ubuntu 14.04.4 LTS
2GB / 2CPU
EDITED
After a while managed to replace ImageMagick on GhostScript
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r240 -sOutputFile=\"file.png\" file.pdf but can't understand how to scale image and crop it.
crop with ImageMagick generates ~35GB temporary files and takes more time than previously.
I managed to resolve my problem that way:
$info = exec("identify -ping %w {$original_pdf_file}"); preg_match('/(\d+x\d+)/', $info, $matches);
"gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r{$r} -g{$dim} -dPDFFitPage -sOutputFile=\"{$png}\" {$filename}"
"convert ".escapeshellarg($png)." -gravity center -background none -extent {$ex}x{$ex} ".escapeshellarg($png)
"convert ".escapeshellarg($png)." -crop {$tiles}x{$tiles}! +repage ".escapeshellarg(str_replace(".png", "_%d.png", $png))
where:
$filename = file.pdf
$png = file.png
$r = 120
$ex = 4000
$dim = $matches[1]
Step:
gives me dimension of original file after what i can play with size of png in the future
converts pdf to png with size i need with aspect ratio
converts png to size i wish with aspect ratio 1:1
cropping everything
this process takes 27.59s on my machine with image resolution 4000x4000 and size of file - only 1,4MB & 0-30MB of temporary files.
Its superconfusing for me, since pdfmaker and postscript are doing same, but in practice coding style is quite different.
I know how to make a line with 2 circles at its end, with moveto and lineto and arc command in Postscript language, however, apparently I have to move to pdfmark due to hyperlinks, pdfmark manual is super un-understandable, and there is no other reference(book/online tutorial).
So, I would be appreciate, if one could generate such thing (as my figure shows) with a little description.
Here's the most simplest version possible. This creates a clickable area in the bottom left corner of the PDF that goes off to a URL.
[/Rect [ 0 0 200 200 ] % Draw a rectangle
/Action % Define an action
<<
/Subtype /URI % Define the action's subtype as a hyperlink
/URI (http://www.example.com/) % Set the URL
>>
/Subtype /Link % Set the type of this PDFmark to a link
/ANN pdfmark % Add the annotation
By default a border will be drawn so you might want to clear that out:
[/Rect [ 0 0 200 200 ] % Draw a rectangle
/Action % Define an action
<<
/Subtype /URI % Define the action's subtype as a hyperlink
/URI (http://www.example.com/) % Set the URL
>>
/Border [0 0 0] % Remove the border
/Subtype /Link % Set the type of this PDFmark to a link
/ANN pdfmark % Add the annotation
This only creates a clickable area, however. You then need to draw some text to click on:
/Helvetica findfont 16 scalefont setfont % Set the font to Helvetica 16pt
5 100 moveto % Set the drawing location
(http://www.example.com/) show % Show some text
Lastly, pdfmark isn't technically defined within the standard so they recommend that if you're not using Adobe's Distiller that you define something to handle it. This code will basically just ignore pdfmark if the compiler doesn't recognize it:
/pdfmark where
{pop}
{
/globaldict where
{ pop globaldict }
{ userdict }
ifelse
/pdfmark /cleartomark load put
}
ifelse
And here's a full working PostScript program:
%!PS-Adobe-1.0
/pdfmark where
{pop}
{
/globaldict where
{ pop globaldict }
{ userdict }
ifelse
/pdfmark /cleartomark load put
}
ifelse
[/Rect [ 0 0 200 200 ] % Draw a rectangle
/Action % Define an action
<<
/Subtype /URI % Define the action's subtype as a hyperlink
/URI (http://www.example.com/) % Set the URL
>>
/Border [0 0 0] % Remove the border
/Subtype /Link % Set the type of this PDFmark to a link
/ANN pdfmark % Add the annotation
/Helvetica findfont 16 scalefont setfont % Set the font to Helvetica 16pt
5 100 moveto % Set the drawing location
(http://www.example.com/) show % Show some text
showpage
EDIT
Also, check out this manual for more in-depth instructions on pdfmark
EDIT 2
Also, also, I should point out that I've spaced things out for instructional purposes. In most cases you'll see the /Action written as a single line such as:
/Action << /Subtype /URI /URI (http://www.example.com/) >>