I would like to add an advanced print form into a customer web site. They have an online web form but they also want to have the form break apart into sections as PDF files so it's distributor print out only the sections they need. The full order form is about 15 pages, but sometimes distributors only use 2 to 4 pages to order.

My propose is to have a web-gadget with a list of all the pdf's with selection boxes, then the distributor check the box and press print and will automatically print just the selected documents.

I'm using PHP/Javascript. Is there any function to print an unopened document such as Word or PDF's files?

It seems that you are trying to go beyond what you can control from the server using PHP. Normally, when you use a PDF document you provide a link, the document opens in the browser and then you have the option of printing it.

If you do a search on javascript pdf print you will find posts on what you can do from javascript to handle printing of a PDF. Trying to print multiple PDFs in one go may be a challenge. If that isn't possible / easy, then you might have to live with pre-packaging certain parts of the form together in a discrete number of bundled PDFs. Another approach would be to generate a custom PDF file based on their requested options then just give them a link to open it and use the standard PDF print capability to print it. Doing it that way gives them the option to save it and print it later (may be useful if they use that same version of the form regularly).

If you go for a javascript solution, you will have check for limitations on which Browsers and which versions of the Browsers the code will run in and if it needs the latest Acrobat version installed.

Thanks for the advice, I will look into it... I might probably will have to see into JavaScript as you mentioned or I could use ajax to request a PHP loop to print an array created by the selection boxes. That just a crazy idea that hit me while reading your suggestion. What do you think?

For example, I could send the file list into an array hold in the values of the selection boxes. Then use a <pre>foreach()</pre> loop to print each file. Actually I don't need Ajax, I can submit the form then execute a print selection function in the application page. In the control panel then use JavaScript to let the customer preview a thumbnail of the file to be print.

I will try to look information for PDF handling function with JavaScript, when I get the code I publish it here as a thank you.

PDFTK (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) could solve most if not all of your difficulty. The only caveat being its a command line utility so you have to interact with it in that fashion. Which would also require server access to install it.

it sounds like your solution would be rather simple though, start with a single pdf file that represents each section of the order form. User selects which sections they need and that in turn is passed to pdftk which takes the files for only those sections, merges them down into a single temporary pdf and then pushes that pdf to the user's browser with a header, or even takes the user to a page that has an embedded pdf control and they can save the pdf or print it from there.

commented: Is a good solution, if you have access to the server! +1

Sound like a great solution, but as you said is the command line utility that needs to be processed to install it in the server, which is not possible for me until the customer finish the process of installing it's own hosting server. Still is discussion, :(

But I found some classes and functions that might help in the meantime, at least to create and print PDF files from a web page.

FPDF seems to be a quick solution, I bump into it today...http://http://www.fpdf.org/ and it has a automatic print solution too in the following link http://www.fpdf.org/en/script/script36.php.

Apparently these functions allow developers to generate a workaround with this situation.

I tried some codes posted in other forums like:

<embed id="samplePDF" src="mypdf.pdf" type="application/pdf"></embed>
<script>
var pdf = document.getElementById('samplePDF');
pdf.print();
</script>

But sincerely it doesn't work for me at all, at least not in Firefox browser, I didn't test it for IE. Nope, I just try it and didn't work either, at least with IE8 in XP.

I will test this FPDF to see how it works a let you know about it!

Or my other workaround would be talking to Bizland to see if they run the command line for me...??? dream on!

Thanks for the suggestion, I will try it though in my local server at home.

OK, this is the code to print a PDF file using JavaScript/PHP cross browser... well I just test it in Firefox and IE, but I use a code for Safari, so I guess it works in Safari at least MacOS

<?php

$browser_ver = get_browser(null,true);
//echo $browser_ver['browser'];

if($browser_ver['browser'] == 'IE') {
?>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pdf print test</title>
<style>
html { height:100%; }
</style>
<script>
function printIt(id){
var pdf = document.getElementById("samplePDF");
pdf.click();
pdf.setActive();
pdf.focus();
pdf.print();
}
</script>
</head>

<body style="margin:0; height:100%;">

<embed id="samplePDF" type="application/pdf" src="/pdfs/2010/dash_fdm350.pdf" width="100%" height="100%" />
<button onClick="printIt('samplePDF')">Print</button>


</body>
</html>

<?php
} else {
?>
<HTML>
<script Language="javascript">

function printfile(id)
{ 
window.frames[id].focus();
window.frames[id].print();
} 

</script>
<BODY marginheight="0" marginwidth="0">

<iframe src="/pdfs/2010/dash_fdm350.pdf" id="objAdobePrint" name="objAdobePrint" height="95%" width="100%" frameborder=0></iframe><br>

<input type="button" value="Print" onclick="javascript: printfile('objAdobePrint');">

</BODY>
</HTML>
<?php
}
?>

base on this code I think a can generate a loop to print, the obstacle now is that the customer has to click print for each page... but with the use of FPDF with FPDI I can create a PDF file extracting pages from the requested PDF files then set print true in the PDF JavaScript as indicated in the API Reference guide from Adobe Acrobat 8.

As I was reading in the FPDF manual you can embed javascript into a PDF file. Now I only need to figure out how to do this dynamically with PHP and Javascript.

But for the moment I think this will help some of many people looking how to print a PDF file using JavaScript in Firefox.

Re-Edit :)

<?php
$browser_ver = get_browser(null,true);
 
if($browser_ver['browser'] == 'IE') 
{
?>
    <!DOCTYPE html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>pdf print test</title>
    
    <style>
    html { height:100%; }
    </style>
    
    <script>
    function printIt(id){
		var pdf = document.getElementById("samplePDF");
		pdf.click();
		pdf.setActive();
		pdf.focus();
		pdf.print();
    }
    </script>
    
    </head>
    <body style="margin:0; height:100%;">
    <embed id="samplePDF" type="application/pdf" src="Building Flash Web Sites For Dummies.pdf" width="100%" height="100%" />
    </body>
    </html>
     
    <?php
    } 
	else 
	{
    ?>
        <HTML>
        
        <script Language="javascript">
        function printfile(id)
        { 
			window.frames[id].focus();
			window.frames[id].print();
        }
        </script>
        
        <BODY marginheight="0" marginwidth="0">
        <iframe src="Building Flash Web Sites For Dummies.pdf" id="objAdobePrint" name="objAdobePrint" height="95%" width="100%" frameborder=0>
        </iframe><br>
        </BODY>
        </HTML>
<?php
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.