Home > data-vis > supporting_functions > export_fig > eps2pdf.m

eps2pdf

PURPOSE ^

EPS2PDF Convert an eps file to pdf format using ghostscript

SYNOPSIS ^

function eps2pdf(source, dest, crop, append, gray, quality, gs_options)

DESCRIPTION ^

EPS2PDF  Convert an eps file to pdf format using ghostscript

 Examples:
   eps2pdf source dest
   eps2pdf(source, dest, crop)
   eps2pdf(source, dest, crop, append)
   eps2pdf(source, dest, crop, append, gray)
   eps2pdf(source, dest, crop, append, gray, quality)
   eps2pdf(source, dest, crop, append, gray, quality, gs_options)

 This function converts an eps file to pdf format. The output can be
 optionally cropped and also converted to grayscale. If the output pdf
 file already exists then the eps file can optionally be appended as a new
 page on the end of the eps file. The level of bitmap compression can also
 optionally be set.

 This function requires that you have ghostscript installed on your
 system. Ghostscript can be downloaded from: http://www.ghostscript.com

 Inputs:
   source  - filename of the source eps file to convert. The filename is
             assumed to already have the extension ".eps".
   dest    - filename of the destination pdf file. The filename is assumed
             to already have the extension ".pdf".
   crop    - boolean indicating whether to crop the borders off the pdf.
             Default: true.
   append  - boolean indicating whether the eps should be appended to the
             end of the pdf as a new page (if the pdf exists already).
             Default: false.
   gray    - boolean indicating whether the output pdf should be grayscale
             or not. Default: false.
   quality - scalar indicating the level of image bitmap quality to
             output. A larger value gives a higher quality. quality > 100
             gives lossless output. Default: ghostscript prepress default.
   gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If
                multiple options are needed, enclose in call array: {'-a','-b'}

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function eps2pdf(source, dest, crop, append, gray, quality, gs_options)
0002 %EPS2PDF  Convert an eps file to pdf format using ghostscript
0003 %
0004 % Examples:
0005 %   eps2pdf source dest
0006 %   eps2pdf(source, dest, crop)
0007 %   eps2pdf(source, dest, crop, append)
0008 %   eps2pdf(source, dest, crop, append, gray)
0009 %   eps2pdf(source, dest, crop, append, gray, quality)
0010 %   eps2pdf(source, dest, crop, append, gray, quality, gs_options)
0011 %
0012 % This function converts an eps file to pdf format. The output can be
0013 % optionally cropped and also converted to grayscale. If the output pdf
0014 % file already exists then the eps file can optionally be appended as a new
0015 % page on the end of the eps file. The level of bitmap compression can also
0016 % optionally be set.
0017 %
0018 % This function requires that you have ghostscript installed on your
0019 % system. Ghostscript can be downloaded from: http://www.ghostscript.com
0020 %
0021 % Inputs:
0022 %   source  - filename of the source eps file to convert. The filename is
0023 %             assumed to already have the extension ".eps".
0024 %   dest    - filename of the destination pdf file. The filename is assumed
0025 %             to already have the extension ".pdf".
0026 %   crop    - boolean indicating whether to crop the borders off the pdf.
0027 %             Default: true.
0028 %   append  - boolean indicating whether the eps should be appended to the
0029 %             end of the pdf as a new page (if the pdf exists already).
0030 %             Default: false.
0031 %   gray    - boolean indicating whether the output pdf should be grayscale
0032 %             or not. Default: false.
0033 %   quality - scalar indicating the level of image bitmap quality to
0034 %             output. A larger value gives a higher quality. quality > 100
0035 %             gives lossless output. Default: ghostscript prepress default.
0036 %   gs_options - optional ghostscript options (e.g.: '-dNoOutputFonts'). If
0037 %                multiple options are needed, enclose in call array: {'-a','-b'}
0038 
0039 % Copyright (C) Oliver Woodford 2009-2014, Yair Altman 2015-
0040 
0041 % Suggestion of appending pdf files provided by Matt C at:
0042 % http://www.mathworks.com/matlabcentral/fileexchange/23629
0043 
0044 % Thank you to Fabio Viola for pointing out compression artifacts, leading
0045 % to the quality setting.
0046 % Thank you to Scott for pointing out the subsampling of very small images,
0047 % which was fixed for lossless compression settings.
0048 
0049 % 9/12/2011 Pass font path to ghostscript.
0050 % 26/02/15: If temp dir is not writable, use the dest folder for temp
0051 %           destination files (Javier Paredes)
0052 % 28/02/15: Enable users to specify optional ghostscript options (issue #36)
0053 % 01/03/15: Upon GS error, retry without the -sFONTPATH= option (this might solve
0054 %           some /findfont errors according to James Rankin, FEX Comment 23/01/15)
0055 % 23/06/15: Added extra debug info in case of ghostscript error; code indentation
0056 % 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
0057 % 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
0058 
0059     % Intialise the options string for ghostscript
0060     options = ['-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '"'];
0061     % Set crop option
0062     if nargin < 3 || crop
0063         options = [options ' -dEPSCrop'];
0064     end
0065     % Set the font path
0066     fp = font_path();
0067     if ~isempty(fp)
0068         options = [options ' -sFONTPATH="' fp '"'];
0069     end
0070     % Set the grayscale option
0071     if nargin > 4 && gray
0072         options = [options ' -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray'];
0073     end
0074     % Set the bitmap quality
0075     if nargin > 5 && ~isempty(quality)
0076         options = [options ' -dAutoFilterColorImages=false -dAutoFilterGrayImages=false'];
0077         if quality > 100
0078             options = [options ' -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -c ".setpdfwrite << /ColorImageDownsampleThreshold 10 /GrayImageDownsampleThreshold 10 >> setdistillerparams"'];
0079         else
0080             options = [options ' -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode'];
0081             v = 1 + (quality < 80);
0082             quality = 1 - quality / 100;
0083             s = sprintf('<< /QFactor %.2f /Blend 1 /HSample [%d 1 1 %d] /VSample [%d 1 1 %d] >>', quality, v, v, v, v);
0084             options = sprintf('%s -c ".setpdfwrite << /ColorImageDict %s /GrayImageDict %s >> setdistillerparams"', options, s, s);
0085         end
0086     end
0087     % Enable users to specify optional ghostscript options (issue #36)
0088     if nargin > 6 && ~isempty(gs_options)
0089         if iscell(gs_options)
0090             gs_options = sprintf(' %s',gs_options{:});
0091         elseif ~ischar(gs_options)
0092             error('gs_options input argument must be a string or cell-array of strings');
0093         else
0094             gs_options = [' ' gs_options];
0095         end
0096         options = [options gs_options];
0097     end
0098     % Check if the output file exists
0099     if nargin > 3 && append && exist(dest, 'file') == 2
0100         % File exists - append current figure to the end
0101         tmp_nam = tempname;
0102         try
0103             % Ensure that the temp dir is writable (Javier Paredes 26/2/15)
0104             fid = fopen(tmp_nam,'w');
0105             fwrite(fid,1);
0106             fclose(fid);
0107             delete(tmp_nam);
0108         catch
0109             % Temp dir is not writable, so use the dest folder
0110             [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
0111             fpath = fileparts(dest);
0112             tmp_nam = fullfile(fpath,[fname fext]);
0113         end
0114         % Copy the file
0115         copyfile(dest, tmp_nam);
0116         % Add the output file names
0117         options = [options ' -f "' tmp_nam '" "' source '"'];
0118         try
0119             % Convert to pdf using ghostscript
0120             [status, message] = ghostscript(options);
0121         catch me
0122             % Delete the intermediate file
0123             delete(tmp_nam);
0124             rethrow(me);
0125         end
0126         % Delete the intermediate file
0127         delete(tmp_nam);
0128     else
0129         % File doesn't exist or should be over-written
0130         % Add the output file names
0131         options = [options ' -f "' source '"'];
0132         % Convert to pdf using ghostscript
0133         [status, message] = ghostscript(options);
0134     end
0135     % Check for error
0136     if status
0137         % Retry without the -sFONTPATH= option (this might solve some GS
0138         % /findfont errors according to James Rankin, FEX Comment 23/01/15)
0139         orig_options = options;
0140         if ~isempty(fp)
0141             options = regexprep(options, ' -sFONTPATH=[^ ]+ ',' ');
0142             status = ghostscript(options);
0143             if ~status, return; end  % hurray! (no error)
0144         end
0145         % Report error
0146         if isempty(message)
0147             error('Unable to generate pdf. Check destination directory is writable.');
0148         elseif ~isempty(strfind(message,'/typecheck in /findfont'))
0149             % Suggest a workaround for issue #41 (missing font path)
0150             font_name = strtrim(regexprep(message,'.*Operand stack:\s*(.*)\s*Execution.*','$1'));
0151             fprintf(2, 'Ghostscript error: could not find the following font(s): %s\n', font_name);
0152             fpath = fileparts(mfilename('fullpath'));
0153             gs_fonts_file = fullfile(fpath, '.ignore', 'gs_font_path.txt');
0154             fprintf(2, '  try to add the font''s folder to your %s file\n\n', gs_fonts_file);
0155             error('export_fig error');
0156         else
0157             fprintf(2, '\nGhostscript error: perhaps %s is open by another application\n', dest);
0158             if ~isempty(gs_options)
0159                 fprintf(2, '  or maybe the%s option(s) are not accepted by your GS version\n', gs_options);
0160             end
0161             fprintf(2, 'Ghostscript options: %s\n\n', orig_options);
0162             error(message);
0163         end
0164     end
0165 end
0166 
0167 % Function to return (and create, where necessary) the font path
0168 function fp = font_path()
0169     fp = user_string('gs_font_path');
0170     if ~isempty(fp)
0171         return
0172     end
0173     % Create the path
0174     % Start with the default path
0175     fp = getenv('GS_FONTPATH');
0176     % Add on the typical directories for a given OS
0177     if ispc
0178         if ~isempty(fp)
0179             fp = [fp ';'];
0180         end
0181         fp = [fp getenv('WINDIR') filesep 'Fonts'];
0182     else
0183         if ~isempty(fp)
0184             fp = [fp ':'];
0185         end
0186         fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
0187     end
0188     user_string('gs_font_path', fp);
0189 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005