{"id":6631,"date":"2016-02-12T09:00:36","date_gmt":"2016-02-12T14:00:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6631"},"modified":"2016-02-12T04:12:32","modified_gmt":"2016-02-12T09:12:32","slug":"creating-multi-page-pdfs","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/02\/12\/creating-multi-page-pdfs\/","title":{"rendered":"Creating Multi-Page PDFs"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31215-append-pdfs\"><tt>append_pdfs<\/tt><\/a> (most up-to-date version included in <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23629-export-fig\"><tt>export_fig<\/tt><\/a>) by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1073021\">Oliver Woodford<\/a>.<\/p><p>Today, I'd like to highlight an entry that I mentioned during one of the customer trainings I gave a few months ago. It's from one of our regular MATLAB Central (and MATLAB) user, Oliver, whom many of you may know.<\/p><p>In the training, we were talking about exporting figures to various formats, and one of the attendees wanted to export them as multi-page PDFs. The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/print.html\"><tt>print<\/tt><\/a> function has an option for exporting a figure as a single-page PDF. It also has an option for exporting multi-page PostScript files. So all you need to do is convert the multi-page PostScript file to a PDF. The initial solution I provided was to use an external program, like Ghostscript, to do the conversion. This is actually how I've always done this back when I was in grad school.<\/p><p>Just having learned about \"automation\" in the training, the attendee wanted to automate this process in MATLAB. I'm sure it's possible, I thought, but I went straight to the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/\">File Exchange<\/a>. There were actually <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?sort=relevancy&amp;term=multi+page+pdf\">a few entries<\/a> that looked promising. The one that met my needs was Oliver's. You still do need to install Ghostscript separately, but once you do that, the tool seemed to automatically find it and use it to combine multiple PDFs.<\/p><pre class=\"codeinput\"><span class=\"comment\">% Create 3 figures and save them individually to PDFs<\/span>\r\nfigure\r\npeaks\r\nprint <span class=\"string\">-dpdf<\/span> <span class=\"string\">fig1.pdf<\/span>\r\n\r\nfigure\r\nspy\r\nprint <span class=\"string\">-dpdf<\/span> <span class=\"string\">fig2.pdf<\/span>\r\n\r\nfigure\r\nplot(rand(10,3))\r\nprint <span class=\"string\">-dpdf<\/span> <span class=\"string\">fig3.pdf<\/span>\r\n\r\n<span class=\"comment\">% Combine three PDFs into one<\/span>\r\nappend_pdfs <span class=\"string\">allfigs.pdf<\/span> <span class=\"string\">fig1.pdf<\/span> <span class=\"string\">fig2.pdf<\/span> <span class=\"string\">fig3.pdf<\/span>\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_appendpdf\/append_pdf_anim.gif\" alt=\"\"> <\/p><p>As I mentioned in the beginning, this functionality is rolled into the ever-so-popular <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23629-export-fig\"><tt>export_fig<\/tt><\/a>, for which Oliver was the original author. Oliver mentions that the one in <tt>export_fig<\/tt> is the maintained version, so I suggest downloading <tt>export_fig<\/tt>.<\/p><p>I do want to put a plug in for <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46203-multiple-page-pdf\">Multiple Page PDF<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1914802\">James Herman<\/a> and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/19-delta-sigma-toolbox\"><tt>ps2pdf<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1034498\">Richard Quist<\/a>. They both use ghostscript that comes with MATLAB, so you don't need to install anything additional. In R2015b, MATLAB changed how PDFs are generated, so these entries don't work any more, but if you're using an older release of MATLAB, definitely take a look.<\/p><p><b>Comments<\/b><\/p><p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6631#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31215-append-pdfs#comments\">comment<\/a> for Oliver.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_55e60b18cb934fc2ab95d5b88ae5836a() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='55e60b18cb934fc2ab95d5b88ae5836a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 55e60b18cb934fc2ab95d5b88ae5836a';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_55e60b18cb934fc2ab95d5b88ae5836a()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\n55e60b18cb934fc2ab95d5b88ae5836a ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31215-append-pdfs |append_pdfs|>\r\n% (most up-to-date version included in\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23629-export-fig |export_fig|>)\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1073021 Oliver\r\n% Woodford>.\r\n%\r\n% Today, I'd like to highlight an entry that I mentioned during one of the\r\n% customer trainings I gave a few months ago. It's from one of our regular\r\n% MATLAB Central (and MATLAB) user, Oliver, whom many of you may know.\r\n%\r\n% In the training, we were talking about exporting figures to various\r\n% formats, and one of the attendees wanted to export them as multi-page\r\n% PDFs. The <https:\/\/www.mathworks.com\/help\/matlab\/ref\/print.html |print|>\r\n% function has an option for exporting a figure as a single-page PDF. It\r\n% also has an option for exporting multi-page PostScript files. So all you\r\n% need to do is convert the multi-page PostScript file to a PDF. The\r\n% initial solution I provided was to use an external program, like\r\n% Ghostscript, to do the conversion. This is actually how I've always done\r\n% this back when I was in grad school.\r\n%\r\n% Just having learned about \"automation\" in the training, the attendee\r\n% wanted to automate this process in MATLAB. I'm sure it's possible, I\r\n% thought, but I went straight to the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/ File Exchange>.\r\n% There were actually\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?sort=relevancy&term=multi+page+pdf\r\n% a few entries> that looked promising. The one that met my needs was\r\n% Oliver's. You still do need to install Ghostscript separately, but once\r\n% you do that, the tool seemed to automatically find it and use it to\r\n% combine multiple PDFs.\r\n\r\n% Create 3 figures and save them individually to PDFs\r\nfigure\r\npeaks\r\nprint -dpdf fig1.pdf\r\n\r\nfigure\r\nspy\r\nprint -dpdf fig2.pdf\r\n\r\nfigure\r\nplot(rand(10,3))\r\nprint -dpdf fig3.pdf\r\n\r\n% Combine three PDFs into one\r\nappend_pdfs allfigs.pdf fig1.pdf fig2.pdf fig3.pdf\r\n\r\n%%\r\n%\r\n% <<append_pdf_anim.gif>>\r\n%\r\n% As I mentioned in the beginning, this functionality is rolled into the\r\n% ever-so-popular\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23629-export-fig |export_fig|>,\r\n% for which Oliver was the original author. Oliver mentions that the\r\n% one in |export_fig| is the maintained version, so I suggest downloading\r\n% |export_fig|.\r\n%\r\n% I do want to put a plug in for\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46203-multiple-page-pdf Multiple Page\r\n% PDF> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1914802\r\n% James Herman> and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/19-delta-sigma-toolbox |ps2pdf|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1034498 Richard\r\n% Quist>. They both use ghostscript that comes with MATLAB, so you don't\r\n% need to install anything additional. In R2015b, MATLAB changed how PDFs\r\n% are generated, so these entries don't work any more, but if you're using\r\n% an older release of MATLAB, definitely take a look.\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6631#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31215-append-pdfs#comments\r\n% comment> for Oliver.\r\n\r\n##### SOURCE END ##### 55e60b18cb934fc2ab95d5b88ae5836a\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_appendpdf\/append_pdf_anim.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro's pick this week is append_pdfs (most up-to-date version included in export_fig) by Oliver Woodford.Today, I'd like to highlight an entry that I mentioned during one of the customer... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/02\/12\/creating-multi-page-pdfs\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6631"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6631"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6631\/revisions"}],"predecessor-version":[{"id":6638,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6631\/revisions\/6638"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}