2

I am using the winnovative html to pdf converter for creating a pdf document. I've added a checkbox for the user to choose wether they would like to print or email the pdf file.

But i can't get the pdf page to open the printer dialog. I have tried the PrinterDialog class but this didn't work, also sending some javascript with window.print() didn't work. I've searched the internet but couldn't find anything.

My page containing the PDF has the following code:

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=Offerte.pdf");
Response.BufferOutput = true;
Response.AddHeader("Content-Length", downloadBytes.Length.ToString());
Response.BinaryWrite(downloadBytes); //downloadBytes = the byte array created by winnovative converter
Response.End();

This will open the pdf viewer inside the browser containing my page as PDF. From here the user is able to click the print button of the pdf viewer/browser. But i would like to have my page open the printer dialog or send the bytes directly to the printer, to minimalize the actions a user has to do.

Any ideas?

oberfreak
  • 1,799
  • 13
  • 20
Pedryk
  • 1,643
  • 1
  • 13
  • 28
  • 2
    Chack this answer http://stackoverflow.com/a/2495430/293712 – Maheep Dec 02 '11 at 11:38
  • calling window.print() onload of body should work. Can you show your code for printing ? – mehul9595 Dec 02 '11 at 11:53
  • I don't have any specific code for printing. Just want the browser to open the printer dialog. I have tried adding window.print() to my header, the body onload and inside the body of html code before the conversion. This all didn't work. Even tried to put it in the page template header of my page in the CMS. And tried to run it outside my CMS. This all didn't work. – Pedryk Dec 02 '11 at 12:03

3 Answers3

1

Since you are streaming the PDF, you have limited options.

I think the best way is to use this approach: https://stackoverflow.com/a/2495430/293712. Open the PDF in a new window (which this new window can stream it). You could then potentially call window.print from the parent window (if you use window.open to open it), and even close the window when done.

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Well, i am open for suggestions. You know a way to send the byte[] directly to the printer? – Pedryk Dec 02 '11 at 14:17
  • The page that will open in the new window; you use the same streaming code. However, the JS executes form the parent page, not the printable page. That's a way to work around it... – Brian Mains Dec 02 '11 at 14:56
  • I think one of the issues is since you change the content type, it can't process the JavaScript (since the content type is PDF) but the parent window could... – Brian Mains Dec 02 '11 at 14:57
  • I should also clarify I'm theorizing here, I've not actually done this. – Brian Mains Dec 02 '11 at 14:57
0

You can actually add an Acrobat JavaScript code to be executed when the PDF document is opened in a viewer. You can see a working example in Execute Acrobat JavaScript Code when Document is Opened demo whenyou select the Open Print Dialog option. The relevant C# code from that demo is copied below:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    Document pdfDocument = null;
    try
    {
        // Convert a HTML page to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);

        string javaScript = null;
        if (alertMessageRadioButton.Checked)
        {
            // JavaScript to display an alert mesage 
            javaScript = String.Format("app.alert(\"{0}\")", alertMessageTextBox.Text);
        }
        else if (printDialogRadioButton.Checked)
        {
            // JavaScript to open the print dialog
            javaScript = "print()";
        }
        else if (zoomLevelRadioButton.Checked)
        {
            // JavaScript to set an initial zoom level 
            javaScript = String.Format("zoom={0}", int.Parse(zoomLevelTextBox.Text));
        }

        // Set the JavaScript action
        pdfDocument.OpenAction.Action = new PdfActionJavaScript(javaScript);

        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Execute_Acrobat_JavaScript.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }
}
Winnovative
  • 118
  • 7
0

Solved it this morning, appeared to be just a stupid mistake. The winnovative converter has a parameter for enabling scripts which is by default set to false. Setting this to true enabled me to use javascript from inside the pdf.

After reading the post which was suggested to me, I found that it had to be possible to use javascript inside the PDF. After searching some more, including the FAQ from winnovative I added the following code:

pdfConverter.ScriptsEnabled = true;
pdfConverter.ScriptsEnabledInImage = true;
pdfConverter.InternetSecurityZone = InternetSecurityZone.LocalMachine;

Then the javascript inside the header worked!

<script type="text/javascript">
window.print();
</script>
Pedryk
  • 1,643
  • 1
  • 13
  • 28