Click here to Skip to main content
15,440,179 members
Articles / Programming Languages / C# 3.5
Tip/Trick
Posted 15 Jul 2015

Stats

42K views
1.1K downloads
3 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
15 Jul 2015CPOL2 min read
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)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDO NOT USE THIS Pin
Member 1294928013-Jan-17 12:43
MemberMember 1294928013-Jan-17 12:43 
AnswerRe: DO NOT USE THIS Pin
SoMad13-Jan-17 12:50
professionalSoMad13-Jan-17 12:50 
QuestionImages are not visible in PDF Pin
Member 1280707424-Oct-16 20:45
MemberMember 1280707424-Oct-16 20:45 
QuestionThrowing Error PDF Content is Not Found Pin
Member 1280707424-Oct-16 1:21
MemberMember 1280707424-Oct-16 1:21 
AnswerRe: Throwing Error PDF Content is Not Found Pin
Member 1280707424-Oct-16 2:22
MemberMember 1280707424-Oct-16 2:22 
QuestionLooking for source code for the plugin.... Pin
Yared Hassene8-Sep-16 7:10
MemberYared Hassene8-Sep-16 7:10 
Question"Also, it is free to use if you don’t need to create a lot of PDF." Pin
Illyrien8-Sep-16 2:59
MemberIllyrien8-Sep-16 2:59 
QuestionDidn’t receive a response from the TC.Xrm.PDFCreator.Plugins.PdfRequest.PrePdfRequestCreate plug-in within the 2:20-minute limit Pin
Yared Hassene2-Sep-16 7:59
MemberYared Hassene2-Sep-16 7:59 
QuestionHow to use tc pdf create Pin
Member 124375076-Apr-16 23:00
MemberMember 124375076-Apr-16 23:00 
AnswerRe: How to use tc pdf create Pin
Yared Hassene2-Sep-16 10:00
MemberYared Hassene2-Sep-16 10:00 
QuestionLicensing file? Pin
Member 120437938-Oct-15 10:59
MemberMember 120437938-Oct-15 10:59 
GeneralMy vote of 5 Pin
Member 1184292016-Jul-15 11:21
MemberMember 1184292016-Jul-15 11:21 
Questionvote 5! good approach Pin
Member 1184287716-Jul-15 10:31
MemberMember 1184287716-Jul-15 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.