Showing posts with label printing. Show all posts
Showing posts with label printing. Show all posts

Thursday, February 17, 2011

Data Dynamics Reports: One touch printing on the web

Preface
Some time ago I blogged around ideas on no-touch printing on the web in ActiveReports. A lot of the new features were added in both of ActiveReports and Data Dynamics Reports since from that blog post was published. Particularly we have updated PDF export capabilities so that they allow performing one touch printing easily. In this article I will explain it in details.

The problem
Let's imagine that we have the simple Html page. It hosts the list of the product categories(obtained from Northwind database, Categories table) and "Print" button. We need to implement the following functionality: a user selects the category, clicks Print, the server returns the PDF document that shows the list of the products from the selected category(obtained from Northwind database, Products table), PDF document starts printing immediately - a user should get the print dialog of PDF viewer and the document should not be opened in the viewer.

One-touch printing in action
Let's create the web-site that implements the functionality described in "Problem" section:
products.xml(change the extension to Rdlx) is the report that accepts category id parameter and displays the list of the products accordingly. Copy products.rdlx in the root folder of the web-site.
Now copy default.htm in the root folder of the web-site and let's look at the page body:

<body>
<label>Select the category to print the list of the product of:</label><br />
<select id="categoryList">
<option value="1">Beverages</option>
<option value="2">Condiments</option>
<option value="3">Confections</option>
<option value="4">Dairy Products</option>
<option value="5">Grains/Cereals</option>
<option value="6">GMeat/Poultry</option>
<option value="7">Produce</option>
<option value="8">Seafood</option>
</select>
<input id="btnPrint" type="button" value="Print" />
<script>
$("#btnPrint").click(function() {
var catId = $("#categoryList").val();
$("#pdfContentPlaceHolder").attr("src", "GetData.aspx?catID="+catId);
});
</script>
<iframe style="visibility:hidden" id="pdfContentPlaceHolder" width="0" height="0" />
</body>

So, in the handler of of Print button click we set the source of the invisible frame to the data that are returned by GetData.aspx page. Once the document is loaded in the frame, the printing should start immediately. How is it achieved? Copy GetData.aspx in the root folder of the web-site and let's look at its Page_Load code:

void Page_Load(object sender, EventArgs e)
{
int catId;
if(!int.TryParse(Request.QueryString["catId"], out catId))
{
Response.Write("Invalid category Id parametere value! No output you will get");
return;
}
var fi = new FileInfo(Server.MapPath("~/Products.rdlx"));
var def = new ReportDefinition(fi);
var runtime = new ReportRuntime(def);
runtime.Parameters[0].CurrentValue = catId;
var pdf = new PdfRenderingExtension();
var streams = new MemoryStreamProvider();
var settings = (pdf as IConfigurable).GetSupportedSettings().GetSettings();
settings["PrintOnOpen"] = "true";
runtime.Render(pdf, streams, settings);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=products.pdf");
using(var stream = streams.GetPrimaryStream().OpenStream())
{
var len = (int)stream.Length;
var bytes = new byte[len];
stream.Read(bytes, 0, len);
Response.BinaryWrite(bytes);
}
}

The code is quite trivial, there are two relatively new features in use though:

  • MemoryStreamProvider allows to export the reports to the memory instead of the disk files. It is pretty convenient for web-scenarios where you don't want to use file system aggressively.

  • We set PrintOnOpen setting of PDF export to true. It will force PDF document to start printing immediately once it is loaded in the invisible frame on default.htm

The task is completed.


ActiveReports
It is possible to use the same technology in ActiveReports. The setting for PDF export filter document option is called OnlyForPrint, it is hidden in the intellisence, but you can use it:

PdfExport pdf = new PdfExport();
pdf.PdfDocumentOptions.OnlyForPrint=true;

Sunday, February 7, 2010

A few thoughts about no-touch printing in ActiveReports6

Preface
I am not sure what is the proper term for the functionality that allows to print a document with no user interaction. In this post I am using "no-touch printing" term. For instance: Every morning Jon Smith turns on his computer at the office and runs his favorite internet browser. The browser opens the start page which is the company web-site. The start page of the company web-site indicates that Jon Smith has logged in and prints the daily schedule on the default printer that is set up on Jon's computer. Let's see how we can implement such scenario using ActiveReports6.

ActiveX Viewer And Flash Viewer.
ActiveX Viewer was introduced in the COM version of the product. ActiveReports was growing up, .NET framework became the product environment, but ActiveX Viewer never was dead in the water, and with good reason! ActiveX Viewer provides developer with the rich capabilities of displaying and printing the reports on the web. For example it's pretty easy to implement no-touch printing on the web with ActiveX Viewer. The code should load the report in the viewer, then wait until the loading is finished and then call PrintReport method. The knowledge base of ActiveReports has several articles that show the sample of such code.
Of course the main disadvantage of ActiveX Viewer is it's ActiveX component. ActiveX components officially operate only with IE and Windows. In our days the usage share of IE is only ~60%. Developers need more uniform technology that allows displaying and printing reports on the web. In ActiveReports6 ActiveX viewer is not supported but Flash Viewer is introduced instead.
Flash Viewer operates with any browser and any OS where the flash player is installed.
The bad news are Flash Viewer does not support no-touch printing.
So, it seems that there are no ways to implement no-touch printing on the web with ActiveReports6? Not exactly!

Turn around.
To display PDF documents in browser Adobe Acrobat Reader includes the Acrobat ActiveX control for Internet Explorer and the Adobe PDF Plug-In For Firefox and Netscape. ActiveX control or FF plug-in can be hosted by the HTML page and a PDF document can be loaded and printed using Javascript or Visual Basic code of that page. I am confident that no-touch printing is possible to implement with Acrobat ActiveX control and I believe that it's possible with Adobe FF plug-in as well. In other hand ActiveReports6 includes the RPX handler which can return the report is saved in PDF format...No more words! Here is sample of code which performs no-touch printing of the report using Acrobat ActiveX control:

<html>
<body onload="window.setTimeout('em.PrintAll();', 5000);">
<div id="PdfContainer">
<span style="font-size: large;">Please wait for a few seconds and the printing will be starting.</span>
<embed id="em" width="0" height="0" src="HelloWorld.rpx?OutputFormat=PDF" />
</div>
</body>
</html>

The code uses EMBED tag to host the PDF document which is returned by RPX handler. PrintAll method prints the entire document without displaying a user dialog box. There are two issues with that code. First of all, it uses the timeout approach to wait until the document is loaded in PDF viewer. I think it's possible to intercept the event which is raised by PDF control when the document loading is completed, I just don't know the exact way to do that. The second issue is the warning which appears before printing starts:

Once "Don't show this message again" checkbox is unchecked, the code provides the no-touch printing, really.

Great Silver Hope
Eventually we will add the Silverlight-based Reports Viewer for ActiveReports. At the moment the most recent version of Silverlight is v.4 Beta 2 and it's new features list includes:
Comprehensive printing support enabling hardcopy reports and documents as well as a virtual print view, independent of screen content.
Unfortunately, that "comprehensive printing support" does not include no-touch printing. The great review of Silverligh 4 printing is available here.
Perhaps the final version of Silverlight 4 or 5 will support no-touch printing. Then we will add the Silverlight-based Reports Viewer and no-touch printing will be fully supported in ActiveReports. That's what I called "Great Silver Hope":)