Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Create/Generate PDF with CRM 2013/2015 Plugin/JavaScript

5.00/5 (2 votes)
15 Jul 2015CPOL2 min read 48.7K   1.1K  
Create PDF from CRM 2013/2015 online/on-prem with Jscript / plugin. All in the solution file, no custom page or Silverlight.

Introduction

I recently had an interesting situation with one of our clients who wanted to send out a PDF invoice from CRM 2015 online. I did some research, one approach seems to be doable, use JavaScript to download a copy of SSRS report and save as PDF, but this is a hack to the report viewer, and it is not supported by Microsoft. Also, the client wants to display customer’s signature on the PDF. I found another solution to convert SSRS into PDF using workflow custom code activity, but it only works with on premise CRM, not CRM online. The client is using Microsoft CRM online 2015. The solution needs be able to run in isolated sandbox. Is it possible?

Using the Code

I keep on researching, I find out that I might be able to use a plugin to convert the HTML to PDF with a PDF tool. The first step is to create a pdfcreate request entity and enclose all logic of creating the PDF into the entity precreate plugin.

Here is the example solution that you can create PDF from JScript, plugin or Workflow custom activity. Also, it is free to use if you don’t need to create a lot of PDF.

With this tool, we need to create the HTML first, however, currently, don’t expect fancy style in the HTML can be converted to PDF. Also, ensure that all tags need to be closed or self-closed, such as <H1>*****</H1>, <br/>, <img *****/>. Right now, only inline CSS is supported. Please refer to the below chart to see supported CSS style.

Step 1

Download the zip.

Step 2

Import the crm solution file, you should be able to create PDF using the PDF request entity. (setting->pdf request).

Step 3

Open the sample source code (currently only C#) in Visual Studio, make sure CRM developer toolkit is installed.

C#
 tc_pdfrequest pdfrequest = new tc_pdfrequest();
 pdfrequest.tc_Content = htmlcontent;
 pdfrequest.tc_CreatePDF = true; //Create the PDF and attach to the pdf request record as a note
 pdfrequest.tc_email = new EntityReference("email",_emailId); //attach the pdf to email
 pdfrequest.tc_objecttype = account.LogicalName; 	// attach to the record type, if null, 
							// won't attach to a record
 pdfrequest.tc_objectid = account.Id.ToString(); 	// attach to the record id, if null, 
							// won't attach to a record
 pdfrequest.tc_Filename = "account_test_pdf.pdf"; 	// PDF file name
 pdfrequest.tc_subject = "Account motification alert"; 	// subject of the email attachment or note
 pdfrequest.tc_name = "Account motification alert"; 	//just a name for the request
 localContext.Trace("Generate a pdf request 123..."); 

service.Create(pdfrequest);

Step 4

Deploy sample source code and run.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)