Ghostscript PDF to cropped TIFF - ghostscript

I need to convert a PDF to a cropped TIFF file. For now I have the following command, which crop nothing...
gs -q -dNOPAUSE -r600 -sCompression=lzw -sDEVICE=tiff32nc -sOutputFile=a2.tiff Orange_facture_721368628_20180301.pdf -dDEVICEWIDTHPOINTS=200 -dDEVICEHEIGHTPOINTS=100 -dFIXEDMEDIA -c quit
Any help ?
Thanks

You've put the media size selection (DEVICE....POINTS and FIXEDMEDIA) after the input filename, you need to put them before. Unless you plan on processing multiple files, or have some specialist need to run PostScript after processing the input, the input filename must be the last thing on the command line.
Don't use -c quit, if you want Ghostscript to exit on completion use -dBATCH.

Related

Piping an image retrieved using curl to sips to change format without saving intermediate file

I have url links to image files I want to retrieve from the internet.
I can download the files using curl without issue using:
curl "https://...web address..." > myfileName;
The image files are of various types, some .bmp some .jpg etc. I have been using sip in Terminal on Mac osx to convert each to .png files using:
sips -s format png downloadFileName --out newFileName.png
This works well on files I've saved as downloadedFileName regardless of the starting file type.
As I have many files to process I wanted to pipe the output of the curl download directly into sips, without saving an intermediate file.
I tried the following (which combines my two working steps without the intermediate file name):
curl "https://...web address..." | sips -s format png --out fileName.png
And get a no file error: Error 4: no file was specified.
I've searched the sip man pages but cannot find a reference for piped input and have been unable to find a useful answer searching SO or google.
Is there a way to process an image downloaded using curl directly in sips without first saving the file?
I do not necessarily need the solution to use a pipe, or even be on one line. I have a script that will cycle through a few thousand urls and simply want to avoid saving lots of files that will be deleted a line later.
I should add, I do not necessarily need to use sips either. However, any solution must be able to handle image files of unknown type (which sips does admirably) as no file extension is present on the files.
Thanks
I don't have sips installed but its
manpage indicates that it cannot read
from stdin. However, if you use Bash or ZSH (MacOS default now) you
can use process substitution, in this example I use convert which is
a part of ImageMagick and can convert different image types too:
$ convert <(curl -s https://i.kym-cdn.com/entries/icons/mobile/000/018/012/this_is_fine.jpg) this_is_fine.png
$ file this_is_fine.png
this_is_fine.png: PNG image data, 800 x 450, 8-bit/color RGB, non-interlaced
After doing that this_is_fine.png will be the only file in the
directory with no temporary files
Apparently sips only reads regular files which makes it impossible to use /dev/stdin or named pipes.
However, it is possible using the mature and feature-rich convert command:
$ curl -sL https://picsum.photos/200.jpg | convert - newFilename.png
$ file newFilename.png
newFilename.png: PNG image data, 200 x 200, 8-bit/color RGB, non-interlaced
(First install ImageMagick via brewinstall imagemagick or sudoportinstall ImageMagick.)
ImageMagick permits image data to be read and written from the standard streams STDIN (standard in) and STDOUT (standard out), respectively, using a pseudo-filename of -.
source, section STDIN, STDOUT, and file descriptors

complete noob, How do I make a script to convert individual pages of a PDF as images AND save them in folders with the same name as the PDF?

For example, if I have book1.pdf and book2.pdf, I would like to create a script where the pages of the pdfs are converted to images and are saveed in their separate folders: book1 folder and book2 folder.
It's something this program does but I do not want to pay 27 bucks just for this.
I'm a complete noob when it comes to coding. I installed Ghostscript and added a printer that runs ghostscript, so now I do have the option of opening a PDF (or any document), and print using the Ghostscript printer, and it outputs the resulting images to a folder.
This is the code for printer properties->ports->arguments for this program:
-sDEVICE=jpeg -r300 -dJPEGQ=100 -o -dSAFER -sOutputFile="C:\IMAGEfiles\image%%03d.jpg" -
My goal now is to automate the system so that I can have a list of PDFs and convert their pages into images and sorted into folders based on the same name as the PDFs. Thank you
This isn't a Ghostscript question really, this is a shell script programming problem.
Since you are using C: I'm assuming you are on Windows. I'm also going to assume you have created the folders in advance.
If you then open a command shell and do :
for %s in (*.pdf) do "c:\program files\gs\gs9.52\bin\gswin64c" -sDEVICE=jpeg -r300 -dJPEGQ=100 -dBATCH -dNOPAUSE -sOutputFile=c:/%~ns/image%03d.jpg %s
That will find all the files with names of the form *.pdf, execute Ghostscript (you may have to alter the paths and executable name, it depends on the version you installed) and output the resulting JPG files to a folder whose names is the '*' part of the input filename.
Note that your original command line has both -o and -sOutputFile, you should modify it to remove one or the other. -o is supposed to be followed by the name of the output file and includes -dBATCH and -dNOPAUSE all wrapped up as one. Whereas -sOutputFile= just sets the output filename. Using both is a bad idea, if it works I'm surprised, and it certainly wouldn't surprise me if it stopped working at some point, or had unexpected side effects.

How to append output file name using input file name without extention while using ghostscript?

I'm using GS to "compress" PDF with 2 clicks. I've added a context menu in windows register with abovementioned code.
For instance if I use it on test.pdf the output file will be test.pdf-compressed.pdf. It works, but I would like to get rid of extention in the filename. Is there any way to do so?
I've tryied to use cmd arguments, but it does not seem to work with the postscript.
C:\Program Files\gs\gs9.27\bin\gswin64c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dCompatibilityLevel=1.4 -sOutputFile=%1%-compressed.pdf -c .setpdfwrite -f %1
This isn't a Ghostscript question. If you get the arguments correct then the parameters passed to Ghostscript will be correct and the output file will be what you want.
You haven't said what you've tried, so that makes it pretty hard to make suggestions. However you should be able to use %~dp1 and/or %~n1 instead of simply %1 to expand to just a path or file. There are other variations, typing "help for" at the Windows command line will give you more details.
Note as always that Ghostscript does not compress PDF files, by using -dPDFSETTINSG=/ebook you are producing a brand-new PDF file which has altered the content from the original (image will be downsampled for example).
Also the sequence -c .setpdfwrite -f has been redundant for years, you don't need it.
[EDIT]
This batch file demonstrates the use of the command shell variable expansion in a batch file
#ECHO OFF
ECHO Input file is %1
ECHO Input directory is %~dp1
\ghostpdl\debugbin\gswin32c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=%~dp1\new.pdf %1
When saved as test.bat and then test d:\temp\input.ps the following output is generated:
Input file is d:\temp\input.ps
Input directory is d:\temp\
GPL Ghostscript GIT PRERELEASE 9.28 (2019-04-04)
Copyright (C) 2019 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
And a file new.pdf is created in the directory d:\temp
So the variable expansion works as expected, because it happens before the command line is executed.
If it still doesn't work for you, then you're going to have to provide more information. In your place I would start by removing the -dNOPAUSE and -dBATCH switches from the command line, at least that way you'll be able to see if Ghostscript is trying to tell you something.
I've clearly stumbled upon this posting a bit late.
However, I wanted to post my answer in case someone comes looking for a solution to a similar issue, in the future.
I started by creating a new Folder on my Desktop, titled "PDF", in which I placed the "test.pdf" File.
I then created a .BAT File, titled "CompressPDF.bat", to which I added the Script below.
This Script will Loop through and Compress Any/All .PDF Files, that are placed in the "PDF" Folder.
It then correctly appends the "-compressed.pdf" string to the File Name, thereby saving the "test.pdf" File as "test-compressed.pdf", per the request of the OP.
As you will Notice, I have Added the "PAUSE" Command at the very end of the Script.
This will keep the Window from Automatically Closing until you Press Any Key, which will allow you to review any Errors that may have arisen, during the compression process.
#echo off
cd "%USERPROFILE%\Desktop\PDF"
for %%f in (*.pdf) do (
gswin64c.exe -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="%%~nf-compressed.pdf" "%%f"
)
PAUSE
I hope this post is able to help others, who may be looking for an answer to a similar issue.
Please, feel free to response or to reach out to me, if anyone has any questions, as I am always happy to help.

How to resize an .eps file using ghostscript

How do you resize .eps file using ghostscript or some other command line utility available for macOS or ruby? I would like to specify the resolution such as 1000x1000 and not the scale.
Similar to this post but resizing with a specified resolution How to resize an EPS file with free software or command line utility
Thanks #KenS for the help. For reference this is the command I ended up using.
gs -o input.eps -dEPSFitPage -sDEVICE=eps2write -c "<</Install {10 10 scale}>> setpagedevice" -f output.eps

How to split PDFs (with applescript)

Does anyone know how to use the PDF kit thing to split pdfs in apple script, as i would like to split my pdf documents in to pairs of uncoloured and some colour pages.
I have tried pdftk, as i was orignally writing a bash script, but it fails on my document, which was produced from LaTeX.
I'd look at installing Ghostscript via MacPorts or Fink. Ghost script has pretty simple command line arguments for doing what you want. You can then control it within an Applescript script.
Typically to split a pdf with ghostscript you do the following:
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dFirstPage=m -dLastPage=n -sOutputFile=out.pdf in.pdf
Where m and n are page numbers.
You can merge pdfs with
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf *.pdf
Automator has a "PDF to Images" choice which extracts all of the pages into individual pdf files... try that.

Resources