File Exchange

image thumbnail


version (16.9 KB) by Richard Quist
Convert PostScript file to PDF using Ghostscript; can generate multi-page PDF files.


Updated 01 Sep 2016

View Version History

View License

Converts a postscript file into PDF. The resulting PDF file will contain one page for each page defined in the postscript file, so a multi-page postscript file, like those generated by using the '-append' option of MATLAB's print command, can be used to generate a multi-page PDF file.
Works with the version of Ghostscript shipped with MATLAB, if found, or with a user-specified version.

Also works with older MATLAB versions, such as R2006b.

Cite As

Richard Quist (2021). ps2pdf (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (40)

Nicola Trotta

Sorry, I've found the problem. In the 'gscommand' I called the "ps2pdf" file instead of "gs" file. Works like a charm, many thanks !


Hi, I have downloaded the toolbox and run it with ghostscript v9.53. Matlab does not give error but the output.pdf created is a one blank page.
Here is the commande line I use : "ps2pdf('psfile', '', 'pdffile', 'output.pdf', 'gscommand','/Applications/','gsfontpath','/Applications/','gslibpath','/Applications/','gspapersize','a4')"
Any thoughts about that ? Thanks in advance

Robert Turner

I have a issue with ps2pdf
Here is the error:
Undefined function or variable 'ps2pdf'.

Error in plotRxModuleATPResults_Rev3>plotPDF (line 645)
ps2pdf('plotFile' , 'Test_File','gscommand','C:\Program Files\gs\gs9.27\bin\gswin32.exe','gsfontpath','C:\Program Files\gs\fonts',
'gslibpath','C:\Program Files\gs\gs9.27\lib');

Error in plotRxModuleATPResults_Rev3 (line 151)
if plotPDFs, plotPDF(plotFile); end

My Code:
ps2pdf('plotFile' , 'Test_File','gscommand','C:\Program Files\gs\gs9.27\bin\gswin32.exe','gsfontpath','C:\Program Files\gs\fonts', 'gslibpath','C:\Program Files\gs\gs9.27\lib');

Robert Turner

Ghostscrip is loaded on my system.

Nicholas Savage

I found a small bug/typing mistake:

line 154 reads: error('ps2pdf:ghostscript', '%s', [ 'GhostScript could not create ''' gsData.pfdFile '''.' ])
Should be: error('ps2pdf:ghostscript', '%s', [ 'GhostScript could not create ''' gsData.pdfFile '''.' ])

Nicholas Savage

Martino Ferretti

Sudhanshu Nahata

Thanks a lot Josh. That worked! :)

Sara Lehmann

Josh Payne


I installed ghost script locally to my (64bit) machine. The exectable I downloaded and installed is "gs921w64.exe".

Here is the command I used to get it working.

ps2pdf('psfile', fullfile(cd,''), 'pdffile', fullfile(cd,'test.pdf'), 'gspapersize', 'letter',...
'gscommand','C:\Program Files\gs\gs9.21\bin\gswin64.exe',...
'gsfontpath','C:\Program Files\gs\gs9.21\lib',...
'gslibpath','C:\Program Files\gs\gs9.21\lib');

This will convert the file to test.pdf. I hope this helps

Sudhanshu Nahata

Can anyone guide me how to install ghostscript to run this file on MATLAB 2016b. I installed ghostscript in the "R2016b/sys/ghostscript" folder. While executing the ps2pdf function, I am getting this error:

"Error using ps2pdf (line 141)
Problem converting PostScript. System returned error: 1.'""' is not recognized as an internal or external command,
operable program or batch file."

Please advise,


I was able to get this working in 2016a by:
* Installing ghostscript from
* using the 'gscommand' and 'gslibpath' arguments to ps2pdf. I get a warning about not specifying 'gsfontpath' but it still works fine.

Note that I also needed to add a '-fillpage' to my print command to get the figures to stay full page. I had just been using 'orient landscape' but some defaults seem to have changed.

Alexander Walzl

Hi @ all
First, i would like to thank you for this well working program.

I use it in connection with the gui2pdf app and gs9.19. Only one question is left. Is there a opportunity to increase the image quality/resolution of the images embedded into the pdf?

Because function-plots are very rough and blurred. Is there a way to fix it?


David Bryson

@ Neal Connardy, i see that builtin fails in 2016(a) any advice?

Neal Conrardy

No more Ghostscript for 2016A?


Great program, thanks. Unfortunately, I can’t make it run on Matlab 2015b. I do get the following error message:

“Error using f_ps2pdf>LocalParseArgs (line 329)
Can not find Ghostscript program in MATLAB

Error in f_ps2pdf (line 112)
gsData = LocalParseArgs(varargin{:});”

Any suggestions what I could do? I read the post of Neal from 08 Jan 2015, but this does not seem to work in Matlab 2015b or lets say I do not have a folder in “sys” that is called “extern”.


To get it to work on my Mac, I changed line 297 to ghostDir = fullfile(matlabroot, 'sys', 'extern', 'maci64', 'ghostscript')`


This does not work on release R2014b. The path to the ghostscript files has been moved.
Line 297 requires updating.
ghostDir = fullfile(matlabroot, 'sys', 'gs8x' );
ghostDir = fullfile(matlabroot, 'sys', 'extern', 'win64', 'ghostscript' );

Andrea Gentilini

Fantastic tool. Would be even better if it allowed user to control for portrait or landscape orientation. I saw other users have requested the same. Thanks, Andrea


great tool anyday!! need help with minor issue. when I have a figure with rotated xticklabels and I append it to a ps file which I make into a pdf using ps2pdf, the figure gets rotated so that the text is now horizontal. any way to force orientation to landscape while writing to pdf? thanks.

David Berns

When i run this program in a folder where the path has a parentheses in it, e.g. C:\DriveName\User(David)\, this program produces the following error: Problem converting PostScript. System returned error: -13.Unrecoverable error: limitcheck in .putdeviceprops.

I indeed tried the program in a folder whose path doesn't have a parentheses and it seems to work fine.


Michael Teo

Oh, i found the solution.

I have to set the MATLAB print to landscape by

orient landscape;

Michael Teo

Can i print the PDF to A4 landscape?

Robert Cumming


Great function! Really useful.
Is there a way to set the PDF paper to horizontal (either letter or A4)? I tried each possible gspapersize, but I didn't find anyone working fine in this case.

Esben Vedel-Larsen

Very nice file, I have found it very usefull. I had the same problem with the version, but the newest file solved it, Thank you!

Esben Vedel-Larsen


Any way to include ps2pdf.m and ghostscript in a compiled application?


Yes, I had the same problem after upgrading to 7.10. I just commented out the if-else starting on line 289, and it will just default to using the internal ghostscript for now.

Can Baran

I think this is pretty well written. One thing to comment:
I just upgraded to 2010a, the line at 287:
if str2double(v.Version) < 7.4
v.version is = 7.10 which is bigger than 7.4 but str2double of 7.10 is smaller than 7.4
So, there might be a better way of dealing with this than the one i am about to write:
compare the first digit(s) before the point, if they are the same, compare the digit(s) after the point. I think this is a still pretty damn solid .m file, I wish i could give 4.5 stars.

Per Bröms

Very nice. This work very good for me.

I found two bugs thou.

1) You need to put " (double quote) around the Ghostscript input file name. If you have space chars in the path e.g. "\\server\directory with space in the name\" it will crash otherwise. So line 123 and 125 need to be modified.

New 123- [s, r] = gsData.cmd(['@' gsData.responseFile], ['"' gsData.psFile '"'], gsDebug);

New 125- [s, r] = system([gsData.cmd ' @' gsData.responseFile ' "' gsData.psFile '"']);

2) To work with really old version of MATLAB, like R14, you need to remove the () after Local_GetOldGhostscript(); in line 288

New 288- [gsCmd, ghostDir] = Local_GetOldGhostscript;

Now it work on R14 and with all directory names.
Also tested on 2006b.


Per Bröms

Andrew Bytheway

Great idea! I did experience problems when using this script in a deployed application (R2009a). The built-in version of Ghostscript doesn't work in deployed applications. It returns the following error:
??? Undefined function or method 'gscript' for input arguments of type 'char'.
Clearly it is failing to recognize the call to gscript altough the function handle was created correctly, and the needed mexw32 file is distributed with the MCR v7.10

Any ideas ???

Tom Mat

The Title (push Ctrl+Shift+D in Acrobat) of the pdf files, is the name and path of the ps File. It would be good to have an option to avoid this...

Tom Mat

Mat K

Great Script, options to set Author, Title, Subject etc. would make it superior...

Pat Murray

Excellent, exactly what I needed, saves me a time-consuming step for my project.


Naveen K

Perfect. Exactly what i needed. Many thanks Richard.

Pablo Pablo

Excelent script.
Tested on Matlab2008a
Thanks a lot.

Yuri Kotliarov

It's amazing! I just started looking for this feature and found this function dated TODAY. Tested on 2007b. Works like a charm. Thanks a lot!

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: gui2pdf

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!