0

I have a web program that dynamically creates a pdf, when a user clicks on the print command in a data grid view, the pdf is created in the page printpdf.aspx. and then the following code is executed:

function printpdf() 
{
    var printWindow = window.open('printpdf.aspx', '', 'height=100,width=200');            
    checkLoad();
    printWindow.focus();
    printWindow.print();
    printWindow.close();      
}

this code is being executed and I get a print dialog box and the form closes on either ok or cancel, however the pdf is not yet generated when this code is triggered. I need a way to wait for page load. or a timer function that would work. I am also using this in multiple browsers. I have tried a timer function with no luck and the onload function.

checkLoad();
function checkLoad() 
{
    if (printWindow.onLoad) 
    {
        printWindow.focus();
        window.print();
        window.close();
    } 
    else 
    {
        setTimeout('checkLoad();', 1000)
    }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

1

The way to do this proper is to add a javascript to the pdf itself telling the pdf to print itself.

You can then load that pdf into a hidden iFrame which you need to clear onbeforeunload to not print again if the user reloads the page with the iFrame.

I have used this method successfully for more than 10 years.

Generate a PDF that automatically prints

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236