You should download the ZIP archive of the entire project to get the full code listing and to try it out.
(13.4 KB, 26 Mar 2010, md5 checksum=9a4a412630366a625f86f593b6c1d392)
Report Markup Language™ is an XML-style language created by ReportLab that describes the precise look of a printed page. This sample application shows how you can use the RML2PDF conversion library from within the Microsoft .NET framework.
It's simple to integrate Report Markup Language into your Microsoft .NET project, and add great looking dynamic PDF output to your .NET based website or desktop application. And you don't need to learn Python to do so - if you can code in HTML, you'll find RML easy to learn. RML lets you add complex and great looking PDF document output to your site, with the minimum of development time.
This sample project shows you how. It's a simple demonstration of how to merge an RML 'template' with user-submitted, variable content - in this case the user's name - and convert the output into a PDF which is returned to the browser.
It's easy to expand on the principles demonstrated here. When merging the template with the variable content, we've used a simple string 'replace' function. However, for more advanced output, users will probably want make use of a template engine such as StringTemplate.NET or NVelocity which will help you construct the RML using loops, conditionals and other programming constructs.
Here's a walkthrough of the most interesting parts of the sample application. The downloadable archive also contains a README file and commented code.
The project serves up a simple HTML form, and the code is all behind the 'click' event for the 'submit' button. First, we set up a Process object from the System.Diagnostics namespace. This is what makes it possible to execute the Python module rml2pdf from inside the .NET application - just give it the name of the python executable.
Dim pythonProcess As Process Dim pythonPSI As ProcessStartInfo pythonPSI = New ProcessStartInfo("python")
Next, read the RML template ('hello.rml') into an IO.StreamReader object, and use the 'replace' function to substitute the user's name with the placeholder in the template.
Dim sw As IO.StreamWriter = New IO.StreamWriter(tmpFileName) Dim txt As String txt = sr.ReadToEnd txt = txt.Replace("##NAME##", name)
The resulting rml file is saved to disk in a temporary location. Then a command line string is constructed in the format "<path\to\rml2pdf.pyc> <path\to\tempRMLfile.rml>" and passed to the process object, which executes Python with that string as an argument. The rml2pdf python module creates the PDF document in the current working directory, and returns the filename, which can be read from the Process.StandardOutput.Readline() function.
pythonProcess = New Process() pythonProcess.StartInfo = pythonPSI pythonProcess.Start() Dim outputPDF As String = System.IO.Path.GetTempPath & pythonProcess.StandardOutput.ReadLine()
Now we have a PDF document in the temp directory, and the only remaining task is to return it to the user. So we get the file into a IO.BinaryReader object, set the appropriate properties in the response object, and use Response.BinaryWrite() to send it back to the browser.
Dim input As New IO.FileStream(outputPDF, IO.FileMode.Open) Dim reader As New IO.BinaryReader(input) Dim bytes() As Byte bytes = reader.ReadBytes(CInt(input.Length)) input.Close() Response.AddHeader("Content-Disposition", "attachment;filename=ReportLabRMLSample.pdf") Response.BinaryWrite(bytes)