M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

PDF Generation

This lesson focuses on the PDF generation option provided in M#. In this lesson we will learn about exporting PDF documents and the basic configuration required.

M# provides a static class PdfService under “MSharp.Framework.Services” namespace, which provides the functionality to generate PDF from HTML.

Exporting PDF

In order to export a PDF document you must have a well formatted document with the relevant data or information. M# uses HTML to PDF generation technique and makes it easier by allowing developers to use Webpages as the input of a PDF document.

In M# you simply need to develop a Webpage with the related data or information you want to export as PDF. Then, in order to export the PDF you just need the “Absolute URL” of that developed Webpage, as shown later in this lesson.

M# allows developers to implement PDF Export and customization by defining the “PDF File Name and PDF customization” attribute on the “Navigate” action of any button. M# “Navigate” action is used to redirect users to the pages developed in a website.

Note: In order to understand this lesson you must have basic knowledge about Entities, Pages and Modules in M#. For more information on those topics please read lesson Entities, Page, Module in chapter 1

The code shown below demonstrates how to implement HTML to PDF export functionality

“GetAbsoluteURL” is an extension method on HttpRequest instance available in M#, which return absolute URL of the specified relative URL

“PageURL” method is implemented in the UserControl class in M#, which is inherited by each and every module in M#. This method accepts the “resource key” of the target page in sitemap and returns the relative URL of the page.

As shown in above, we need to define the “ContentType” and “Header” of the “HttpResponse” instance we are sending to the client browser so that the client browser can perform appropriate actions.

“PdfService.CreateHtml2PdfConverter()” creates an instance of Html 2 PDF converter service. “GetPdfFromUrlBytes” method returns the byte array object holding the given URL’s page html in bytes which is then sent to the client browser in server response.

Html2PDFconverter” class exposes three main public properties to configure a PDF template

1) PdfHeaderOptions

2) PdfDocumentOptions

3) PdfFooterOptions

The above mentioned properties implement further properties to design each section e.g. Font, height, width, margins, quality etc.