GhostScript PS to PDF converting - cropted some parts - python-3.x

I tried to convert Python Tkinter canvas to pdf. For that I used Ghostscript. Here is the code part,
canvas.postscript(file="tmp.ps",colormode='color')
somecommand = "gswin64c -o output.pdf -sDEVICE=pdfwrite -g57750x62070 - dPDFFitPage tmp.ps"
call(somecommand, shell=True)
The output pdf with large size but the pdf shows canvas GUI cropped and it is in bottom left corner of the pdf.
I want to show complete canvas on pdf.

You've specified -dPDFFitPage, but your input file appears to be PostScript (judging by the '.ps' extension and your question title). PDFFitPage works with PDF input. Even using -dPSFitPage of the simpler -dFitPage will only work if the input PostScript program requests a media size. If it doesn't then the interpreter can't tell what its bounding box is, and so cannot scale it to fit the media.
You've also specified a media size in pixels (-g57750x62070) which is entirely inappropriate when the input and output are vector formats. For what it's worth, you are specifying a fixed media size of (approximately) 80 inches by 86 inches, using the default resolution of 720 dpi.
If all you want to do is turn a PostScript file into a PDF file then the simpler:
gs -sDEVICE=pdfwrite -o out.pdf input.ps
is sufficient.

Related

Ghostscript : Crop Certain Area?

I am new to ghostscript.
I have a pdf which contains a card. i want to crop that card out.
Currently with the understanding of document i am only able to convert the pdf to image but have no luck in cropping.
Saw every other related question but there are not working for me.
This is code i used in batch file for converting the pdf to image:
"C:\Program Files\gs\gs9.50\bin\gswin64c.exe" -sDEVICE=png16m -r300 -o c:\users\jen\desktop\pdf.png -f "c:\users\jen\desktop\pdf.pdf
pause
now i don't know how to crop with it too ?
i want to crop at certain postition like: Left:28 Top:524 Width:492.3 Height:161
EDIT
I will be using this in firebase functions.
Example PDF file THE_PDF_TO_CROP. I want to cutout the blue area of pdf to image.
You need to set several parameters; Firstly you need to specify the width and height of the output bitmap. You can use either -dDEVICEHEIGHTPOINTS and -dDEVICEWIDTHPOINTS, or alternatively you can specify the output size in pixels using -g<x>x<y> where and are the number of pixels in the x and y directions. Obviously that will vary depending on the resolution. You can't (obviously) use fractional pixels.
If you use -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS then you also need to set -dFIXEDMEDIA to tell the interpreter not to use the media size from the PDF file instead.
So that shoould create an output bitmap of the correct size. If you try rendering your file using just that, you will see that it renders just a portion of the page from the bottom left. So now you need to shift the content around so that the portion you want lies at the bottom left of the media. You can do that by using the PageOffset PostScript operator.
You haven't given any numbers, nor supplied an example file, so lets say (for the sake of example) that you want to render a 1 inch by 2 inch portion of the document. Lets further say that you the part you want rendered starts 2.5 inches from the left edge, and 1.5 inches from the bottom edge.
A suitable command line would be:
gs -sDEVICEWIDTHPOINTS=72 -dDEVICEHEIGHTPOINTS=144 -dFIXEDMEDIA -r300 -sDEVICE=png16m -o out.png -c "<</PageOffset [-180 -108]>> setpagedevice" -f input.pdf
Note that PDF (and PostScript) units are 1/72 inch so 72 = 1 inch, 144 = 2 inches. You need to shift the origin of the page down and left, which is why the values for PageOffset are negative.
If that doesn't work for you I'll need to see your PDF file and you'll need to tell me which version of Ghostscript you are using.

Postscript to PDF scale to fit into A4

I need to create an A4 PDF file by fitting into page this 13.44x16.44 inches Postscript file. I thought ps2pdf could help me but I cannot get the desired effect.
I use this command to create the PDF:
ps2pdf -dFIXEDMEDIA -dPDFFitPage -sPAPERSIZE=a4 ori.postscript salida.pdf
Please note I used -dFIXEDMEDIA and -dPDFFitPage to force fit the Postscript file into the A4 paper size, but those apparently aren't working.
This is the original file:
Edit: Here's the original file
And this is the resulting file. As you can see, the image isn't resized to fit, but just placed as is:
Firstly; the order of operands in Ghostscript is important, they are applied in the command line order. So you would want to apply the -sPAPERSIZE before you apply -dFIXEDMEDIA and both of those before you apply -dPDFFitPage.
I'd also suggest that you use Ghostscript directly rather than using the ps2pdf script.
If that still doesn't work for you, then you will need to provide an example file to show the problem, I can't tell you anything by looking at pictures.
You should also state the operating system and version of Ghostscript being used.
EDIT
The problem is that your PostScript program doesn't request a media size, it simply draws on whatever media happens to be available at the time. Some programs will rescale their content to fit whatever media is currently available, this isn't one of them. Anything which doesn't lie on the current media is allowed to be clipped off.
The 'FitPage' code relies on the PostScript program requesting a media size, which it then compares to the current (fixed) size. From that it works out how much to scale the content so that it fits into the new media.
If your program doesn't request a media size then there's no way for Ghostscript to know how much to scale it so it fits.
Now your program does have BoundingBox comments, but those are just comments, a PostScript consumer will ignore them. But you can use them.....
You can either modify the header of your PostScript program to pretend its an EPS instead of a PostScirpt program. :
Change
%!PS-Adobe-2.0
To
%!PS-Adobe-2.0 EPSF-3.0
and then use -dEPSFitPage instead of -dPDFFitPage then it will produce something like what (I think) you want. Note that PDFFitPage is for PDF input, so you shouldn't really be using it anyway. For PostScript input you want -dPSFitPage
Alternatively, read the BoundingBox comments and apply a media size request and origin translation yourself.
This command:
gs -sPAPERSIZE=a4 -dFIXEDMEDIA -dPSFitPage -sDEVICE=pdfwrite -sOutputFile=\temp\out.pdf -c "<</PageSize [968 1184]>> setpagedevice -20 -50 translate" -f d:\temp\ori.eps
Produces the same output as treating the file as EPS would.

How to prevent the white border after convert with ghostscript

i try to convert an .eps File to .png with ghostscript.
The .eps file has a resolution of 1000x1000 px. But the outfile has big white borders on left and on the bottom side.
gs -dNOPAUSE -dBATCH -r1000x1000 -q -sDEVICE=png256 -dDEVICEWIDTHPOINTS=880 -dDEVICEHEIGHTPOINTS=720 -sOutputFile=infile.png infile.eps
EPS files don't have a resolution, so it cannot possibly have a resolution of 1000x1000, especially not 1000x1000 pixels, because that's not a resolution, its a size.
I very much doubt you want to set the resolution to 1000 dpi and at the same time set a media size of 880 points x720 points. That will result in a .png 12000x10000 pixels. (There are 72 points to the inch, which means you are setting a media of 12x10 inches at 1000 doits per inch)
The correct way to handle an EPS file (which is slightly but importantly different to a PostScript file) is to arrange the scaling yourself.
If the dimensions of the resulting image are not important to you, then you can use -dEPSCrop which will produce an image where the dimensions of the media are taken from the comments in the EPS file.
If you require that the image has specific dimensions then you should use -g to set the media size (in pixels), set -dFIXEDMEDIA and set -dEPSFitPage which will scale the EPS to fit the dimensions of the media.
I found the solution :
-dEPSCrop
Not sure what is causing that without seeing the eps file, but you can trim it off with ImageMagick like this:
convert SomeFile.png -trim result.png
ImageMagick is installed on most Linux distros and is available for OSX, and Windows.

Using rsvg-convert can one convert multi page or multi file SVG to multi page PDF?

Is there a way to use "rsvg-convert" to convert either a multi page SVG or multi SVG files to a single multi page PDF?
I am using the command:
/usr/bin/rsvg-convert -f pdf -o out.pdf in.svg
you can provide a list of .svg files when converting to pdf.
rsvg-convert -f pdf -o out.pdf file1.svg file2.svg file3.svg
alternatively you can convert all .svg files in a folder to 1 pdf like this:
rsvg-convert -f pdf -o out.pdf *.svg
rsvg-convert accomplishes a multi-SVG to single-PDF task see #holger-will's answer. However, it resizes all of the PDF pages to the same size as the first SVG file (regardless if the second, third, etc. are larger). This is not suitable for combining an assortment of varied SVGs which have different heights and widths.
An alternative -- svgstopdf -- is available on Debian-based systems at https://github.com/pimpreneil/svgstopdf and ported to Windows at https://github.com/flueterflam/svgstopdf
Syntax is similar also:
svgstopdf file1.svg file2.svg (...) out.pdf
or
svgstopdf *.svg out.pdf
The benefit of using svgstopdf is that every PDF page is exactly the size of the corresponding SVG image. You do not need to resize all your SVGs to a uniform width or height! Nor will subsequent SVGs be clipped, if the first one is smaller.
Although the OP asks for rsvg-convert, please note that this method accomplishes the same task. Future users who find this question may be interested to learn of alternatives without the inherent restriction of rsvg-convert (as of August 2018). This is very clear on lines 244-245 of rsvg-convert.c of librsvg see https://github.com/brion/librsvg/blob/master/rsvg-convert.c:
/* in the case of multi-page output, all subsequent SVGs are scaled to the first's size */
rsvg_handle_set_size_callback (rsvg, rsvg_cairo_size_callback, &dimensions, NULL);
Although it says "scaled" in the comment, my tests with using rsvg-convert indicate that the subsequent PDF pages are sized to be the same as the first. Furthermore, the subsequent SVGs are not resized to fit the PDF page but rather clipped. So the "scaling" process is referring to the PDF page and not the SVG image.
An example of this is by taking the two SVG files:
128px x 128 px - https://en.wikipedia.org/wiki/File:Blue_sphere.svg
288px x 288px - https://www.shareicon.net/download/2015/10/03/111553_wifi.svg
Using rsvg-convert -f pdf -o out.pdf Blue_sphere.svg 111553_wifi.svg, the resulting PDF looks like this:
(page 1)
(page 2)
Obviously, this is not the desired outcome. Instead, using svgstopdf.exe Blue_sphere.svg wifi.svg out2.pdf results in:
(page 1)
(page 2)

Extracting Text from a PDF file with embedded font

I have a PDF file containing some tabular data.
http://dl.dropbox.com/u/44235928/sample_rotate-0.pdf
I have to extract the tabular data from it. I have tried following with no success :
Select the text and paste it to notepad/excel-sheet. (I am getting junk characters)
Used save as text from Acrobat Reader. It is also giving junk characters and not the actual text.
Tried ApachePDFBox command line utility to extract text from PDF. It is also giving junk characters instead of real texts.
Finally I am trying a OCR solution. I am converting the pdf file into .tif images using ImageMagick and getting those images processed by tesseract OCR.
The OCR solution is not very accurate though( about 80% words matched ).
I tried changing density and geometry of the image created from PDF to get better results from tesseract OCR.
convert -rotate 90 -geometry 10000 -depth 8 -density 800 sample.pdf img_800_10000.tif;
tesseract img_800_10000.tif img_800_10000.tif nobatch letters;
I am not sure for what kind of image( density, geometry, monochromatic, sharpen boundary etc) would be best suited for the OCR.
Please suggest what could be the best possible parameters(density,geometry,depth etc) for generating images from a PDF file, so that the tesseract accuracy will increase.
I am open to other( non-ocr ) solutions as well.
In this case I recommend to NOT use ImageMagick for the PDF -> TIFF conversion. Instead, use Ghostscript. Two reasons:
Using Ghostscript directly will give you more control over individual parameters of the conversion.
ImageMagick cannot do that particular conversion itself -- it will call Ghostscript as its 'delegate' anyway, but will not allow you to give all the same fine-grained control that your own Ghostscript command will give you.
Most of the text in the table of your sample PDF is extremely small (I guess, only 4 or 5 pt high). This makes it rather difficult to run a successful OCR unless you increase the resolution considerably.
Ghostscript uses -r72 by default for image format output (such as TIFF). Tesseract works best with r=300 or r=400 -- but only for a font size from 10-12 pt or higher. Therefor, to compensate for the small text size you should make Ghostscript using a resolution of at least 1200 DPI when it renders the PDF to the image.
Also, you'll have to rotate the image so the text displays in the normal reading direction (not bottom -> top).
This is the command which I would try first:
gs \
-o sample.tif \
-sDEVICE=tiffg4 \
-r1200 \
-dAutoRotatePages=/PageByPage \
sample_rotate-0.pdf
You may need to play with variations of the -r1200 parameter (higher or lower) for best results.
Since a comment asked "How to define the geometry of an image when using Ghostscript as we do in convert?", here is an answer:
It does not make sense to define geometry (that is image dimensions) and resolution for a raster image created by Ghostscript at the same time.
Once you convert a vector based page of a given dimension (such as PDF) into a raster image (such as the TIFF G4 format) giving a desired resolution (as done in the other answer), you already indirectly and implicitly also did set the dimension:
The original PDF dimension of your sample file sample_rotate-0.pdf is 1008x612 points.
At a resolution of 72 DPI (the default Ghostscript uses if not given directly, or -r72 in the Ghostscript command if given directly) the image dimensions will be 1008x612 pixels.
At a resolution of 720 DPI (-r720 in the Ghostscript command) the image dimensions will be 10080x6120 pixels.
At a resolution of 1440 DPI (-r1440 in the Ghostscript command of my other answer) the image dimensions will be 20160x12240 pixels.
At a resolution of 1200 DPI (-r1200 in the Ghostscript command) the image dimensions will be 16800x10200 pixels.
At resolution of 1000 DPI (-r1000 in the Ghostscript command) the image dimensions will be 14000x8500 pixels.
At a resolution of 120 DPI (-r120 in the Ghostscript command) the image dimensions will be 1680x1020 pixels.
At resolution of 100 DPI (-r100 in the Ghostscript command) the image dimensions will be 1400x850 pixels.
If you absolutely insist to specify the dimension/geometry for the output image on the Ghostscript commandline (rather than the resolution), you can do so by adding -gNNNNxMMMM -dPDFFitPage to the commandline.
There you can find decoded content of your file: https://docs.google.com/open?id=0B1YEM-11PerqSHpnb1RQcnJ4cFk
A absolutely sure the OCR is the best way to read pdf file, but you can try REGEX-ing the native content. It going to be be the hard and long way.

Resources