{"id":54,"date":"2010-09-03T15:38:28","date_gmt":"2010-09-03T22:38:28","guid":{"rendered":"http:\/\/www.designersgate.com\/blogs\/?p=54"},"modified":"2010-09-03T15:38:28","modified_gmt":"2010-09-03T22:38:28","slug":"print-pdf-files-with-javascriptphp","status":"publish","type":"post","link":"https:\/\/designersgate.com\/blog\/print-pdf-files-with-javascriptphp\/","title":{"rendered":"Print PDF files with JavaScript\/PHP"},"content":{"rendered":"<p>I started a topic in <a href=\"http:\/\/www.daniweb.com\/forums\/thread293733.html\" target=\"_blank\" rel=\"noopener noreferrer\">DaniWeb<\/a> about how to generate a gadget to let customer choose from a list of pdf files an create a custom order form from them&#8230; I still working on it but here is something I found that will help many of your that are looking for a solution how to print a pdf file with JavaScript. After a lot of googling I finally find some pieces of codes that will help you to complete this task, you can use only JavaScript or you can combine PHP\/JavaScript to do it.<\/p>\n<p>But here i will show you with both programming languages interacting together&#8230;. For this task we need to request the browser information in order to decipher which code to use with Internet Explorer and which to use for the rest of them&#8230;<\/p>\n<pre>&lt;?php\n\n$browser_ver = get_browser(null,true);\n\/\/echo $browser_ver['browser'];\n\nif ($browser_ver['browser'] == 'IE') {\n?&gt;\n\n&lt;!DOCTYPE html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\n&lt;title&gt;pdf print test&lt;\/title&gt;\n&lt;style&gt;\nhtml { height:100%; }\n&lt;\/style&gt;\n&lt;script&gt;\nfunction printIt(id){\nvar pdf = document.getElementById(\"samplePDF\");\npdf.click();\npdf.setActive();\npdf.focus();\npdf.print();\n}\n&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body style=\"margin:0; height:100%;\"&gt;\n\n&lt;embed id=\"samplePDF\" type=\"application\/pdf\" src=\"\/pdfs\/2010\/dash_fdm350.pdf\" width=\"100%\" height=\"100%\" onLoad=\"printIt('samplePDF');\" \/&gt;\n&lt;button onClick=\"printIt('')\"&gt;Print&lt;\/button&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n&lt;?php\n} else {\n?&gt;\n&lt;HTML&gt;\n&lt;script Language=\"javascript\"&gt;\n\nfunction printfile() {\n    window.frames['objAdobePrint'].focus();\n    window.frames['objAdobePrint'].print();\n}\n\n&lt;\/script&gt;\n&lt;BODY marginheight=\"0\" marginwidth=\"0\"&gt;\n\n&lt;iframe src=\"\/pdfs\/2010\/dash_fdm350.pdf\" id=\"objAdobePrint\" name=\"objAdobePrint\" height=\"95%\" width=\"100%\" frameborder=0&gt;&lt;\/iframe&gt;\n\n&lt;input type=\"button\" value=\"Print\" onclick=\"javascript: printfile();\"&gt;\n\n&lt;\/BODY&gt;\n&lt;\/HTML&gt;\n&lt;?php\n}\n?&gt;\n<\/pre>\n<p>As you see there&#8217;s a big difference between the most used browsers (IE 13.5%), (Firefox 29.6%) and (Chrome 50%) so the sad reality that we need to code for all of them. But with the new IE 10, coding it is a bit more easier. This little piece of code might help anyone.<\/p>\n<p>I hope so. I spent all my day searching for solutions for a mayor project. At least this is a big step into it.<\/p>\n<p>Happy Coding<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started a topic in DaniWeb about how to generate a gadget to let customer choose from a list of pdf files an create a custom order form from them&#8230; I still working on it but here is something I found that will help many of your that are looking for a solution how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":871,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[8,32,33,34,16,29,35,36,37],"class_list":["post-54","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-browsers","tag-chrome","tag-firefox","tag-ie","tag-javascript","tag-php","tag-print","tag-project","tag-safari"],"acf":[],"jetpack_featured_media_url":"https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2010\/09\/DESIGNERS-GATE-JS-PrintPDF.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media\/871"}],"wp:attachment":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}