How can I convert .eps file to .pdf in Mathematica? - wolfram-mathematica

How can I convert .eps to .pdf inside Mathematica (perhaps using GhostScript?)?

After installing GhostScript and setting appropriate environment variables (for Windows you should add gs\bin and gs\lib to the PATH, where gs is the top-level Ghostscript directory) you can use Jens Nöckel's method for converting .eps to .pdf (all the glyphs will be outlined):
gsEPS2PDF[epsPath_String, pdfPath_String] :=
Run["gswin64c.exe -sDEVICE=pdfwrite -dNOCACHE -sOutputFile=\"" <>
pdfPath <> "\" -q -dbatch -dNOPAUSE \"" <> epsPath <> "\" -c quit"]
Here gswin64c.exe is the name of GhostScript executable for 64bit Windows systems, for Linux replace it with gs.
Another method based on Kurt Pfeifle' code (without font outlining):
gsEPS2PDFEmbedFonts[epsPath_String, pdfOutputPath_String] :=
Run["gswin64c.exe -sFONTPATH=c:/windows/fonts -o \"" <>
pdfOutputPath <>
"\" -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress \"" <> epsPath <>
"\""]
Here c:/windows/fonts is the directory where fonts are located. See also here for information about GhostScript command line parameters.

gr = Import["file.eps", "eps"]
Export["file.pdf", gr, "pdf"]

Related

Accessing files from Ghostscript commands with -dSAFER

I'm trying to write a number of gs commands for server-side use. The user-provided PDF/JPEG files, I have to work with cannot be assumed to be safe (broken or even malicious files could be provided). Therefore, I'm trying to write all of my Ghostscript commands with -dSAFER, to guarantee at least a basic level of security.
Unfortunately, -dSAFER appears to be incompatible with certain gs commands. Take for example the following command:
# count number of pages in PDF
gs -dQUIET -dBATCH -dNOPAUSE -dNOSAFER -dNODISPLAY \
-c "(input.pdf) (r) file runpdfbegin pdfpagecount = quit"
How would you re-write this command with -dSAFER? The command fails if I just add -dSAFER, because gs can't read the file input.pdf (which is what I expect). How do I tell gs that is permitted to read input.pdf, but nothing else? Maybe there's a way to permit reading of files only from certain directories?
Here's a second example command:
# convert JPEG to single-page PDF
gs -dQUIET -dBATCH -dNOPAUSE -dNOSAFER \
-sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSettings=/printer \
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
-sOutputFile=output.pdf \
viewjpeg.ps \
-c "(input.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore >> setpagedevice viewJPEG"
This command has exactly the same problem as the first one. How do I re-write this with -dSAFER?
Please include a link to the relevant documentation in your answer if you think that -dSAFER is really not needed for my commands.
You can add directories to the search list using -I, IIRC any such directory is permitted to be read. For the current directory you can also use -P- and -P.
See Use.htm in the ghostpdl/doc directory.
However, even if adding the current directory allows you to read the file, this will soon stop working with -dSAFER anyway. There's an ongoing programme to 'harden' the interpreter when -dSAFER is set by hiding/remobing any non-standard operators especially if there's any possibility they could be misused.
Your proposed usage is, simply, incompatible with -dSAFER. The commands you are using will almost certainly be specifically prohibited because they are inherently unsafe. Essentially by messing about inside PDF files like that, your PostScript program is unsafe.
Of course, you know that your PostScript program is safe, and since you are not executing any part of the PDF, the PDF is also safe. The PDF would only be unsafe if you attempted to actually execute the contents, which your program doesn't do. It simply opens the file reads the Pages tree, and tells you the value associated with the Count key.
So you don't need to set -dSAFER in this particular example anyway. However any such program which did execute the PDF content (eg by rendering any part of it, or sending it to the pdfwrite device) would not be safe.
Your second example also doesn't need SAFER, since a JPEG can't contain code to access the disk.

Ghostscript changed Orientation from landscape to portrait

I have a PDF file that I need to change the orientation from landscape to portrait, using this command GS. The Orientation switch it is not changing the orientation. Is there a extra setting I need to add?
::Path to your Ghostscript EXE
set GSC="C:\Program Files (x86)\gs\gs9.16\bin\gswin32.exe"
%GSC% -sDEVICE=pdfwrite -sOutputFile="D:\TRANS\PDF\2_ZNOR_00000000000000000070.pdf" -dNOPAUSE -dEPSCrop -c "<</Orientation 3>> setpagedevice" -f "D:\TRANS\PDF\1_ZNOR_00000000000000000070.pdf"
It worked for me
List<string> switches = new List<string>
{
"-empty",
"-dQUIET",
"-dSAFER",
"-dBATCH",
"-dNOPAUSE",
"-dNOPROMPT",
"-dDEVICEHEIGHTPOINTS=612",
"-dDEVICEWIDTHPOINTS=792",
"-dFIXEDMEDIA",
"-dFitPage",
"-sDEVICE=pdfwrite",
"-dAutoRotatePages=/None",
"-sOutputFile=" + resultFilePath,
"-c",
"<</Orientation 1>> setpagedevice",
"-f"
};
switches.Add(this.FilePath);
Most of the switches you are using there will have no effect at all, and you haven't done anything to rotate the PDF file either.
The first thing to note is that Ghostscript is really the wrong tool for this job, it doesn't modify PDF files, it generates completely new ones. The process is described here
The -dEPSCrop switch only has an effect if the input is an EPS file, otherwise it does nothing.
The operands to setpagedevice are incorrect, you are passing an empty hex string <> when setpagedevice expects a dictionary <<>>. Even then an empty dictionary will do nothing.
I expect you intended to put <</Orientation 1>> setpagedevice but actually that's only a media matching request. In order to get Ghostscript to create a rotated PDF file you would have to alter the media size, set it to fixed, and set -dFitPage. You would also have to set the AutoRotatePages switch to None in order to prevent the automatic reorientation of the output so that the text is horizontal.
Since you haven't supplied the PDF file to look at, nor stated its media size, I can only guess at the sizes. However, something like
gswin32c.exe -sDEVICE=pdfwrite -sOutputFile=new.pdf -dDEVICEHEIGHTPOINTS=612 -dDEVICEWIDTHPOINTS=792 -dFitPage -dAutoRotatePages=/None input.pdf

ghostscript to convert a ps file to pcl loosing tray settings

I'm using the below command to convert ps to pcl but the pcl when printed doesn't have the tray settings anymore, is there anyway of retaining them?
"gswin32c.exe" -sDEVICE=ljet4 -dNOPAUSE -dBATCH -dQUIET -sOutputFile="c:\t.pcl" "c:\t.ps"
e.g. in ps file:-
%%EndPageSetup
<> setpagedevice statusdict begin 1 setpapertray end
Currently, no, the PCL output devices don't carry forward any 'metadata', just the marking content.

Batch convert Powerpoint single-slide presntations to JPEG

In search of Automator or Applescript to batch convert Powerpoint Presentations with a single slide to an image file (preferably JPEG).
I am using Powerpoint 2011 for Mac and I am running OS X Yosemite. A
You could use unoconv which is part of LibreOffice to convert to PDF and then ImageMagick to convert from PDF to JPG.
Both these tools can be installed on OSX using homebrew, i.e.
brew install imagemagick
The command would then be
unoconv someFile.ppt someFile.pdf
convert -density 144 someFile.pdf someFile.jpg
I wouldn't use AppleScript to batch this, I would use find from the bash shell in Terminal like this
find . -iname "*.ppt" -exec unoconv "{}" "{}.pdf" \;
After weeks trying to, I find a way to batch convert an entire folder of pptx files into jpg (one slide each) with the same name as original each file. ITs windows using VBA. He goes, don't mind the non-code part I just changed the code and mix many that I find out there.
Sub BatchSave()
' Opens each PPT in the target folder and saves as PPT97-2003 format
Dim sFolder As String
Dim sPresentationName As String
Dim oPresentation As Presentation
Dim osld As Slide
' Get the foldername:
sFolder = InputBox("Folder containing PPT files to process", "Folder")
If sFolder = "" Then
Exit Sub
End If
' Make sure the folder name has a trailing backslash
If Right$(sFolder, 1) <> "\" Then
sFolder = sFolder & "\"
End If
' Are there PPT files there?
If Len(Dir$(sFolder & "*.PPTX")) = 0 Then
MsgBox "Bad folder name or no PPT files in folder."
Exit Sub
End If
' Open and save the presentations
sPresentationName = Dir$("*.PPTX")
While sPresentationName <> ""
Set osld = ActiveWindow.View.Slide
osld.Export sPresentationName & ".jpg", "jpg"
' New presentation is now saved as N_originalname.ppt
' Now let's rename them - comment out the next couple lines
' if you don't want to do this
' Original.PPT to Original.PPT.OLD
'Name sFolder & sPresentationName As sFolder & sPresentationName & ".OLD"
' N_Original.PPT to Original.PPT
'Name sFolder & "N_" & sPresentationName As sFolder & sPresentationName
sPresentationName = Dir$()
Wend
MsgBox "DONE"
End Sub

Pass parameters to an application

I'm attempting to use ghostscript to set a specific page size for scanned PDF files. I can run the script when it resides in the same folder as the executable but when I move the .vbs file out of the directory I can't get it to work. The .run line is where I'm having problems. The triple quotes for exe path without arguments opens the exe but i can't figure out how to pass the parameters.
I'm sure you can see that I'm pretty new to this.
strInput = InputBox ("Enter 1 for Landscape or 2 for portrait:")
Set objShell = WScript.CreateObject("WScript.Shell")
If strInput=1 then
Call LandScape
ElseIf strInput=2 Then
Call Portrait
Else
MsgBox "Your entry is invalid. Click OK to exit"
End If
Landscape sub-routine:
Sub LandScape
MsgBox "Your images are Landscape"
objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe""& "-dQUIET"&" -dNOPAUSE"&" -dBATCH"&" -dDEVICEWIDTHPOINTS=2592"&" -dDEVICEHEIGHTPOINTS=1728"&" -dFIXEDMEDIA"&" -sDEVICE=pdfwrite"&" -sOutputFile=OUTPUT.pdf"&" INPUT.pdf""
'This line works when in same directory: gswin64c -dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf
End Sub
The 1st parameter to WScript.Shell.Run() is a full command line. So you need to
Add spaces between arguments;
Quote arguments that contain embedded spaces. In VB/VBScript, two consecutive double quotes inside a string literal are interpreted as a literal quote.
Can you now deduce the correct syntax for the objShell.Run line?
The quotes can get confusing so it sometimes helps to use Chr(34) instead and to keep the executable separate from its params.
strExe = "c:\Program Files\gs\gs9.04\bin\gswin64c.exe"
strParams = "-dQUIET -dNOPAUSE -dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf"
objShell.Run Chr(34) & strExe & Chr(34) & " " & strParams

Resources