Convert Images to PDF using GhostScript - ghostscript

I am trying to convert jpg images to PDF using GhostScript. I have installed latest version on ghostscript on my Win7 32bit machine.
gswin32c -sDEVICE=pdfwrite -h -r300 -o C:\Docs\MyFile.pdf
C:\gs\gs9.10\lib\viewjpeg.ps -c C:\Docs\123.jpg viewJPEG
It read and created pdf but it is of size 3KB which doesnt show anything in it.
Is something wrong with the above command?
Solved:
I used following command and it converted my jpeg to PDF
gswin32c -sDEVICE=pdfwrite -o stuff%03d.pdf viewjpeg.ps -c "(C:/Docs/grid.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore >> setpagedevice viewJPEG"

Yes the command is invalid. -h gives you help at the command line, its not sensible to use that in a real command. You've specified -c which means 'process the next stuff as direct PostScript' and then put the filename directly. '\' is an escape character in PostScript, so you'll end up trying to execute a PostScript command 'CDocs123.jpg' there won't be any such command, so you'll get an error. The pdfwrite device has already opened the PDF file, but the error means no marks are drawn on the page, so you get an empty PDF file.
You need to enclose the filename in parentheses so that it is loaded as a PostScript string, and I'd also recommend using forward slashes:
(c:/Docs/123.jpg)

Related

Can I redirect macOS lpr output to a PDF instead of to a printer?

I am looking for a method of sending the output of macOS lpr to a PDF file instead of to a printer.
When I use the lpr command in macOS to print a text file to a printer, it uses the Menlo font for the text, so, clearly, it is not simply sending raw text but applying formatting. I am trying to figure out how to redirect that formatted output to a PDF file instead of to a printer.
I've tried piping the output to pstopdf, but even if that's a possible solution, I can't make it work.
I need to make this a portable solution, so that I can distribute it as part of an app, and therefore I can't require someone else to install brew or any other software on their system. This means I can't use something like enscript which isn't native to macOS to convert a text file to PostScript, then convert that to PDF and print it. I've tried using nenscript but it does strange things to formatting and doesn't produce the correct output the way the lpr command does.
Can anyone suggest a way to get the lpr output into a file?
The answer is:
cupsfilter -i text/plain inputfile > outputfile.pdf

How can I get the page size (print it to stdout) with Ghostscript?

Can I just get the page size with Ghostscript?
I am aware if this answer, but can it be done straight on the command line somewhat like this:
gs -dNODISPLAY -q -c "some postscript" input_file.pdf
as I cannot guarantee that pdf_info.ps is available where the command is run.

Using Ghostscript in Automator?

I'm a bit of a noob in automator, I hope this isn't a silly question.
I have a shell script compdf.sh that takes a pdf as input, flattens and compresses it, and saves it as a new file in the same directory with the suffix '_comp.pdf':
#!/bin/sh
OIFS=$IFS
IFS=‘.’
arr=($1)
prefix=${arr[0]}
suffix='_comp.pdf'
fout=$prefix$suffix
echo "Compressed file to be saved as $fout"
IFS=$OIFS
/usr/local/bin/gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dPDFSETTINGS=/ebook \
-sOutputFile="$fout" "$1"
This script works like a charm in terminal, by issuing the command compdf.sh <input_file>. I want to turn it into a service using automator, but when run it issues an error (simply says "action failed" in the "Run Shell Script" box:
(*Note: I've added "Get Specified Finder Items" so I can see the results in Automator.) Clearly I'm not passing the input to the script properly, yet this page says inputs are passed as $1-$n. Help?
The script requires that you specify the input --> "$#"
/usr/local/bin/compdf.sh "$#"

How do I use Ghostscript?

I'm a novice when it comes to these things, so please, be gentle.
I'm trying to us Ghostscript, but I cant even launch a command. I'm trying to run this little bit of code that someone posted.
gswin32c.exe ^
-o c:/path/to/output.pdf ^
-sDEVICE=pdfwrite ^
-dPDFSettings=/Screen ^
[...more desired parameters (optional)...] ^
/path/to/first.ps ^
/path/to/second.ps ^
/path/to/third.pdf
I've made sure my file paths are correct, but I can't even figure out how to drop down a command line. I have tried launching Ghostscript and the code from command prompt, but Ghostscript gives me an error saying " undefined in -o".
Any help would be greatly appreciated.
First thing you should do is reduce the complexity of your command line. From the command shell try:
gswin32c
Do you get a bunch of GS copyright messages and a 'GS>' prompt ? If you do then type 'quit' then press return and you should be back to the command shell. This means Ghostscript is installed, in your path and working. (Since you got an error message I suspect this will be fine)
After that, try something like:
gswin32c -o out.pdf -sDEVICE=pdfwrite
When you get the 'GS' prompt type 'showpage' and press return. At the next prompt type 'quit' and return.
That should create a PDF file in the current directory called out.pdf with a single blank page.
After that you can build up your command line bit by bit. Note that there is no switch called PDFSettings, its actually called PDFSETTINGS, the command line switches are case-sensitive. I would also firmly recommend that you don't use it. That switch sets a load of parameters (documented in ps2pdf.htm) many of which are probably irrelevant to you and some of which have results you don't want. Work out what you do want and set the parameters individually.

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

Resources