imposition pdf via ghostscript - ghostscript

there are lots of solution, like Quite Imposing. My thoughts, can we use ghostscipt do similar thing. say,
- split pages into several ps files
- using postscript run command, insert those ps file into a temple
- transfer ps template into pdf (via ghostscript again)
here comes my experiment template,
%!
/Times-Roman findfont 14 scalefont setfont
% Page 1
save
/showpage {} bind def
72 200 translate
(BBB001.ps) run
restore
72 680 moveto (This is a text on page 1) show
72 200 translate
72 100 moveto (this page 1) show
showpage
72 100 moveto
(this page 2) show
showpage
after changing above into pdf can't see BBB001.ps, where am I wrong? or it doesn't work completely!
[update]
I finially get it work, turn BBB001.ps to BBB001.eps.

OK first, don't go to PostScript and back to PDF, that's going to lose all kinds of quality.
If you want to do page imposition, then you need to look into the PDF interpreter, which is itself written in PostScript, and tweak it at a fairly high level. Basically you just need to not do the initgraphics/eraspage/showpage dance for each page.
You might like to look at my answer to this question.
Its not really possible to say why you can't see 'BBB001.ps' because I have no idea what's in that PostScript program. If it contains a showpage, then it will render that page eject it, erase the page and then do the remaining part of the program (the text "This is a text on page 1" etc) eject that page, go on to do the next page.

Related

libvips extract area of NDPI from Ex40 not from map

I am using libvips to get pyramids of .ndpi images.
through this answer and searching the documentation I found this command
vips extract_area myimage.ndpi[level=0] mypyramid.dz 0 0 10000 10000
Which extracts a crop starting at 0 0 and size 10000 10000 to a dzi file.
The level parameter is the magnification, 0 is the highest.
The problem is that the ndpi has the following images inside:
myimage_macro.tif
myimage_map.tif
myimage_x0.15625_z0.tif
myimage_x0.625_z0.tif
myimage_x10_z0.tif
myimage_x2.5_z0.tif
myimage_x40_z0.tif
And vips is taking myimage_macro.tif while I need myimage_x40_z0.tif
There should be a parameter like level to choose which from the images inside the OpenSlide (ndpi) I want.
Some people ask. Why not extracting the tif and then running vips?
Well, because vips tells me this:
openslide2vips: opening slide: No such value: directory 0, tag 278
Which means that using ndpisplit to extract the tif is somehow not saving the metadata to allow vips to recognize the image
So I am in a bit of a pesky situation. I have enormous images and I need to extract a slightly less enourmous piece and then have its pyramid.
Please help me, right now I am basically coding it all my self and it works but it is EXTREMELY slow.
I've written this as an answer, though it's not really an answer. It seemed too long as just a comment.
The libvips openslideload operation lets you pick an associated image to load. You can get a list of the associated images from the slide-associated-images metadata tag. For example:
$ vipsheader -f slide-associated-images 2013_09_20_29.ndpi
macro
$ vipsheader -f slide-associated-images CMU-1.svs
label, macro, thumbnail
You then pick out an associated image with perhaps:
$ vips crop CMU-1.svs[associated=label] x.jpg 10 10 100 100
To get a small part of the label.
So ... check what associated images openslide reports for your slide. If you can get the one you need, pick that with the associated parameter. If the image you need is not listed, I would contact the openslide project, since they will need to add support.
You could also check the openslide command-line tools, they might perhaps offer more options.

PostScript, can't see visual output with ghostscript

%!
/Times-Roman 12 selectfont
100 100 moveto
(Hello) show
How can I take the output as PDF and EPS
formatted file.
Your question really needs some editing, its not clear (to me anyway) what your problem is.
You say 'PostScript, can't see visual output with Ghostscript' as the title, and then want to know about 'taking the output'. So which is it ? A problem viewing a PostScript file when using Ghostscript ? Or a problem with the output ?
You can use the eps2write device to get EPS output and the pdfwrite device to get PDF output, as per the documentation. Neither has 'visual output'.

Chrome: How to print PDF with original size (100%, no scaling/shrinking)

Today I encountered one weird bug in Chrome's printing behavior.
When I try to print a dynamically created PDF using Chrome (also in Firefox), every image inside the PDF gets a little bit smaller (8mm).
This doesn't happen neither in Adobe Reader/Acrobat/MacOSX Default Printer Dialog.
Let me explain more about my code so far:
dynamic PDF:
I use prawn to dynamically create a PDF A4 (landscape format) using this code:
require 'prawn'
require "prawn/measurement_extensions"
class CustomPdf < Prawn::Document
def initialize(label) # A4
super({
:page_size => "A4",
:page_layout => :landscape,
:margin => 0,
:print_scaling => :none})
image "my_image.png", :width => 213.mm, :at => [3.mm, 3.mm]
end
end
nested image inside the PDF:
The Image nested inside the PDF has a size of 213mm x 70mm with
300 DPI. In Pixel: 2515x827.
I create the Image using RMagick but the size, dpi and everything so far seems to be alright. So no further explanation at the moment.
Expected result:
The printed PDF will show an image with 213cm width.
Actual result:
The printed PDF will show an image with 205mm width.
What I tried so far
Setting the prawn page_size to something lower than A4 (since the Image isn't bigger as A4 as well...).
Playing around with print_scaling(there are 3 modes, :none, :AppDefault, :something_except_the_other_ones (Read more about it here)
Giving the image a fixed height (70mm)
Playing around with various settings provided by the Chrome Printer Dialog
Tested under Windows Chrome (it's 213mm there, weird), maybe it's related to MacOSX Chrome?
Tested under IE11 (205mm) unfortunately.
Neither of these did anything.
I looked through the Bugreporters of Chromium and found 2 people who might share the same problem as I do:
https://code.google.com/p/chromium/issues/detail?id=97972
https://code.google.com/p/chromium/issues/detail?id=67091
The last one was touched 2013 but no one said anything that actually worked for my case.
Examples
Here is an example 300 DPI image which width is 2515 px:
If Imgur changes the resolution to 72, you can pull the example image from the repository (link: https://github.com/posixpascal/chrome-printing-bug/blob/master/example.png)
Here is an example PDF
Direct Link: Direct Link
GitHub PDF Viewer: https://github.com/posixpascal/chrome-printing-bug/blob/master/output.pdf
Github Repository with example image & pdf generator script
https://github.com/posixpascal/chrome-printing-bug
Unfortunately I can't change the width of the image (not even a pixel).
Someone experienced the same problem?
Any ideas? It's an interesting puzzle I guess... :x
When I print a plain image or a website with Chrome there is a 'Use Original Size' or '100% size' option which I don't have when printing the PDF.
I also tried making a webpage instead of a PDF and set the image width to 213mm which didn't work either...
This is my test results from what i have seen by using a windows system and a Mac ( Yosemite )
Windows :
Printed the PDF in Chome : Perfect - 213 mm
Printed in IE : Perfect - 213 mm. This was less before ( around 205-210ish ). So changed the default print settings :
Selected the Print option -> Used Print Dialog -> Selected the printer -> Print Preferences -> Advanced -> Paper Size -> Changed scaling manually -> tested A4, A5 and so on.-> A5 ( for some reason gave me 213 )
So i moved onto the Mac.
Mac Findings:
Changed Paper Size and tested the same : Size was 213 mm.
This was done like this : Apple Print
So naturally, i tested printing it through Chrome directly instead of opening the PDF through OSX -> Printed at 205 mm.
Tested with printscaling on prawn at :appdefault -> Printed at 213 mm.
Tested the same on Firefox -> lower than 205 - 210 mm.
So i dont really know if its a Chrome issue because its acting weird on Firefox for me.
So ultimately, these are my findings after testing out the code on my end.
I experienced a similar 8 mm shrinkage using Chrome on Windows. Checked all my printer properties and settings and couldn't find what could be doing it. Turns out, I had the option "fit to page" ticked in the Chrome printing (ctrl+p) screen. Un-ticked it and reprinted a page, compared their widths and they were 8 mm off. Sometimes it's the simple things...
In my experience, Chrome prints html usually smaller. It may have to do with WebKit engine having "smart shrinking" feature. I have come across to it here (text search shrinkage). Or, it might be just that, Chrome developers gave it a little tweak since people in the past complained that the stuff does not fit the print.

Can I create a PDF which will always opens at zoom level 100%?

I am running into an issue with PNG to PDF conversion.
Actually I have big PNG files not in size but in contents.
In PDF conversion it creates a big PDF files. I don't have any issue with its quality, but whenever I try to open this PDF in PDF viewer, it opens in "Fit to Page" mode.
So, I can't see the created PDF in the initial view, but I need to zoom it up to 100%.
My question is: can I create a PDF which will always open at zoom 100% ?
You can possibly achieve what you want with the help of Ghostscript.
Ghostscript supports to insert PostScript snippets into its command line parameters via -c "...[PostScript code here]...".
PostScript has a special operator called pdfmark. This operator is not understood by most PostScript interpreters, but is understood by Acrobat Distiller and (for most of its parameters) also by Ghostscript when generating PDFs.
So you could try to insert
-c "[ /PageMode /UseNone /Page 1 /View [/XYZ null null 1] \
/PageLayout /SinglePage /DOCVIEW pdfmark"
into a PDF->PDF conversion Ghostscript command line.
Please take note about various basic things concerning this snippet:
The contents of the command line snippet appears to be 'unbalanced' regarding the [ and ] operators/keywords. But it is not! The initial [ is balanced by the final pdfmark keyword. (Don't ask -- I did not define this syntax...)
The 'inner' [ ... ] brackets delimit an array representing the page /View settings you desire.
Not all PDF viewers do respect the view settings embedded in the PDF file (Acrobat software does!).
Most PDF viewers allow users to override the view settings embedded in PDF files (Acrobat software also does this). That is, you can tell your viewer to never respect any settings from the PDF files it opens, but f.e. to always open it with "fit to width".
Some specific things about this snippet:
The page mode /UseNone means: the document displays without bookmarks or thumbnails. It could be replaced by
/UseOutlines (to display bookmarks also, not just the pages)
/UseThumbs (to display thumbnail images of the pages, not just the pages
/FullScreen (to open document in full screen mode)
The array for the view mode constructed as [/XYZ <left> <top> <zoom>] means: The zoom factor is 1 (=100%), the left distance from the page origin is the special 'null' value, which means to keep the previously user-set value; the top distance from the page origin is also 'null'. This array could be replaced by
/Fit (to adapt the page to the current window size)
/FitB (to adapt the visible page content to the current window size)
/FitH <top>' (to adapt the page width to the current window width);` indicates the required distance from page origin to upper edge of window.
...plus several others I cannot remember right now.
So to change the settings of an existing PDF file, you could do the following:
gs \
-o out.pdf \
-sDEVICE=pdfwrite \
-c "[ /PageMode /UseNone /Page 1 /View [ /XYZ null null 1 ] " \
-c " /PageLayout /SinglePage /DOCVIEW pdfmark" \
-f in.pdf
To check if the Ghostscript command worked, open the PDF in a text editor which is capable of handling binary files. Search for the /View or the /PageMode keywords and check if they are there, inserted as values into the PDF root object.
If it worked, check if your PDF viewer honors the settings. If it doesn't honor them, see if there is an overriding setting within the viewers preference settings.
I did a quick test run on a sample PDF of mine. Here is how the PDF root object's dictionary looks now, checked with the help of pdf-parser.py:
pdf-parser-beta.py -s Catalog a.pdf
obj 1 0
Type: /Catalog
Referencing: 3 0 R, 9 0 R
<<
/Type /Catalog
/Pages 3 0 R
/PageMode /UseNone
/Page 1
/View [/XYZ null null 1]
/PageLayout /SinglePage
/Metadata 9 0 R
>>
To learn more about the pdfmark operator, google for 'pdfmark reference filetype:pdf'. You should be able to find it on the Adobe website and elsewhere:
https://www.google.de/search?q=pdfmark%20reference%20filetype%3Apdf&oq=pdfmark%20reference%20filetype%3Apdf
In order to let ImageMagick create a PDF as you want it, you may be able to hack the file defining your delegate settings. For more help about this topic see for example here:
http://www.imagemagick.org/Usage/files/#delegates
PDF specification supports this functionality in this way: create a GoTo action that goes to first page and sets the zoom level to 100% and then set the action as the document open action.
How exactly you implement it in real life depends very much on the tool you use to create the PDF file. I do not know if ImageMagick can create such actions.

Configuring GhostScript to rotate page output other than multiple of 90 degrees (PDF => PNG)

I'm trying to rotate GhostScript's output from a PDF input using the following:
gs -dSAFER -dBATCH -dNOPAUSE -r200 -sDEVICE=pngmono \
-dAutoRotatePages=/None -sOutputFile=output.png -c 10 rotate -f input.pdf
It generates the output file without any rotation (vs. the desired 10 degree rotation). Any ideas what's going wrong here?
Firstly; AutoRotatePages is only defined for the pdfwrite family of devices, other devices don't do anything with it. So specifying it to the pngmono device will have no effect.
Secondly, the PDF interpreter resets the graphics state when it processes the PDF file. It does this because, in order to do things like page fitting, setting the PageSize to the MediaBox of the PDF file and a bunch of other stuff, it calls setpagedevice. One of the implicit actions of setpagedevice is to call initgraphics, which resets the CTM.
Basically you can't rely on the PostScript graphics state at the time when you start processing a PDF file to have any effect on the graphics state while processing the PDF.
If you really want to do this you will have to modify gs/Resource/Init/pdf_main.ps, at the end of pdfshowpage_setpage:
pop currentdict end setpagedevice
} bind def
You'll need to insert your rotation here, after the setpagedevice. The neatest way to do this is to use a PostScript parameter, say UserRotation. You might then do:
pop currentdict end setpagedevice
/UserRotation where {
/UserRotation get rotate
} if
} bind def
And call GS with -dUserRotation=10
For those running on a system where the Resources are built into the ROM file system, you will need to modify the file on disk and then tell GS to use the modified Resources using the -I switch (-I/ghostpdl/gs/Resource/Init). For anyone trying to use this in Windows, you will first need to get hold of the Resources (they are not currently supplied as part of the Windows binary release) which will probably mean downloading the Ghostscript sources.

Resources