Is it possible to print a report without displaying it?

  • Using SSRS2005, is it possible to print out a report to the user's default printer without displaying anything (the report, the print dialog, etc.)?

    User's just want to click a button or link in their application and have the report print out.

    The application is in C# (VS 2005)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • If you install the Printer Delivery Extension you should be able to call the render method and have it automatically print. Check the link below...

    http://msdn2.microsoft.com/en-us/library/ms160778.aspx

    Hope this helps!


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • This looks very interesting. But it also looks like it only works for networked printers?

    It sure would be nice if there was a way to just print to the user's default printer, whether that be a networked or local printer.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • You should be able to modify the delivery extension to do that...I haven't delved into it so I can't say for sure but from what I understand the idea is you can modify it however you like which should give you what you're looking for...

    Good Luck!


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • What I ended up doing:

    1. Render the report as a PDF, saving it to a file.

    2. Start a process that launches Adobe Acrobat Reader with the print command line option.

    I really think that the ReportViewer control needs to be able to print to the user's default printer without all this hoopla.

    Something simple like:

    ReportViewer.PrintToDefaultPrinter();

    Wayne

    Here's my C# code:

    private void DeleteFile(string FileToDelete)

    {

    if (File.Exists(FileToDelete)) File.Delete(FileToDelete);

    }

    private string RenderAsPDF(Microsoft.Reporting.WinForms.ServerReport Report)

    {

    // this.Text is the name of the report as shown on the form's caption

    string TempFile = Path.GetTempPath() + this.Text + ".pdf";

    string MimeType;

    string FileExt;

    DeleteFile(TempFile);

    FileStream fs = new FileStream(TempFile, System.IO.FileMode.Create);

    reportViewer1.ServerReport.Render("PDF", null, null, fs, out MimeType, out FileExt);

    fs.Close();

    return TempFile;

    }

    private void PrintPDFFile(string FileToPrint)

    {

    RegistryKey pdfKey = Registry.ClassesRoot.OpenSubKey(@"\.pdf");

    string pdfValue;

    if (pdfKey != null)

    {

    pdfValue = (string)pdfKey.GetValue(""); // get the default application for pdf files, ie. AcroExch.Document

    pdfKey = Registry.ClassesRoot.OpenSubKey(@"\" + pdfValue + @"\CurVer");

    if (pdfKey != null)

    {

    pdfValue = (string)pdfKey.GetValue(""); // get the current installed version of the default application, ie. AcroExch.document.7

    pdfKey = Registry.ClassesRoot.OpenSubKey(@"\" + pdfValue + @"\shell\Print\command");

    }

    }

    if (pdfKey == null)

    {

    MessageBox.Show("Unable to print PDF report", "PDF Print error", MessageBoxButtons.OK);

    }

    else

    {

    pdfValue = (string)pdfKey.GetValue("");

    int Position = pdfValue.IndexOf(@"""", 2);

    string FileName = pdfValue.Substring(1, Position - 1);

    System.Diagnostics.Process p = new System.Diagnostics.Process();

    p.StartInfo.FileName = FileName;

    p.StartInfo.Arguments = "/p /h" + @"""" + FileToPrint + @"""";

    p.Start();

    for (int i = 0; i < 5; i++)

    {

    if (!p.HasExited)

    {

    System.Threading.Thread.Sleep(2000);

    p.Refresh();

    }

    else

    break;

    }

    if (!p.HasExited)

    {

    p.CloseMainWindow();

    }

    p.Close();

    }

    }

    Note that with Adobe Reader v8, this leaves Adobe Reader open. If you want to close it, add a timer, set the interval to 1500, and in the timer's Tick event, add:

    Timer1.Enabled = false; // turn off the timer so it doesn't fire again

    // shut down the acrobat reader when it's finished printing the file.

    Process[] processes = Process.GetProcessesByName("AcroRd32");

    foreach (Process p in processes)

    {

    p.WaitForInputIdle();

    p.CloseMainWindow();

    p.Close();

    }

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Nice work around...

    Wayne,

    Just wondering here, why would a user want to print a report without first viewing it? I would think that they would at least want to see what it looks like to do a sanity check before printing something which could be completely erroneous data.


    Cheers,

    Ben Sullins
    bensullins.com
    Beer is my primary key...

  • Ben Sullins (4/21/2008)


    Nice work around...

    Wayne,

    Just wondering here, why would a user want to print a report without first viewing it? I would think that they would at least want to see what it looks like to do a sanity check before printing something which could be completely erroneous data.

    This application is to be used in something like a library... people can check things in and out. However, they always have to sign for what was done. This just prints out the receipt so that it can be signed by the person. They don't want the extra clicks / time involved in previewing a report. (the application shows on the screen the current status - they don't need to see it again)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply