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.
tc_pdfrequest pdfrequest = new tc_pdfrequest();
pdfrequest.tc_Content = htmlcontent;
pdfrequest.tc_CreatePDF = true;
pdfrequest.tc_email = new EntityReference("email",_emailId);
pdfrequest.tc_objecttype = account.LogicalName;
pdfrequest.tc_objectid = account.Id.ToString();
pdfrequest.tc_Filename = "account_test_pdf.pdf";
pdfrequest.tc_subject = "Account motification alert";
pdfrequest.tc_name = "Account motification alert";
localContext.Trace("Generate a pdf request 123...");
service.Create(pdfrequest);
Step 4
Deploy sample source code and run.