Create PDF with CRM Script

Hello guys, 

Is there an easy way to create a PDF using CRMScript and save it as an attachment to the ticket ?

RE: Create PDF with CRM Script

Create a PDF? Containing what? 

Some context please :)

Av: Hans Wilhelmsen 18. okt 2017

RE: Create PDF with CRM Script

Thanks for replying Hans.

The PDF is supposed to have 2 parts. 

1. Customer Information

2. Order details in table format.

Av: Ummair Tahir 18. okt 2017

RE: Create PDF with CRM Script

Hi Ummair, 

we don't have a way to do it with only CRM Script.

The only way, as far as I know, is to use a 3rd party service, or by using a jQuery/JavaScript plugin that needs to be triggered somehow.

We don't have a way to automatically create a PDF and attach it to the ticket in a background process.

 

Edit:

Or there might be a way, by using NetServer to write to a Document Template (although I'm not so familiar with this approach).

Av: Simen Mostuen Iversen 18. okt 2017

RE: Create PDF with CRM Script

Hi, 
you can do it by installing https://wkhtmltopdf.org/ on the same server as CS is installed. Then, create a webpage in Service with the content you want to PDF'ify. Next, call runProgram to call wkhtmltopdf, passing in the URL you want to create a PDF for. 

Av: Frode Lillerud 18. okt 2017

RE: Create PDF with CRM Script

Hi,

Could it perhaps be easier to create an XPS-file in stead of a pdf?

A bit to "hardcore" for me, but doesn't a windows-server have standard components to create an XPS?

Just an idea....

Niels

Av: Niels van Broekhoven 18. okt 2017

RE: Create PDF with CRM Script

I liked your idea Frode :)

Does a call to the program return a filepath or something that I can save in CS ?

Ummair

Av: Ummair Tahir 18. okt 2017

RE: Create PDF with CRM Script

Hi Ummair, 

when you call the wkhtmltopdf.exe file you also pass in a parameter which says where to put the file, and then you can use the File class in ejscript to read the file afterwards. Using that you can save it as an Attachment in Service.

Av: Frode Lillerud 18. okt 2017

RE: Create PDF with CRM Script

Hey Frode, 

I've set up a site on the same server as Service is installed: 

 

After this I'm supposed to run a command like this: 

wkhtmltopdf http://google.com f:\example.pdf

Is it supposed to be called via the URL? i.e http://localhost/pdfCreator/bin/wkhtmltopdf.exe "and the rest ..."

Or do I use javascript to start cmd and do the magic there ? I cant make any of them work actually.

Av: Ummair Tahir 23. okt 2017

RE: Create PDF with CRM Script

Hi Ummair,

wktohtml is just installed like any other application on the server, and you don't need to do anything in IIS for it.

From CRMScript you just use the full path to the program, something like this:

String CreatePDFFromUrl(String url, String outputFullPath, String arguments)
{
 //Requires wkhtmltopdf to be installed on the server (https://code.google.com/p/wkhtmltopdf/)
 //If the PDF file already exists, then it will be overwritten

 String pathToPdfGenerator = "\"c:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\" " + arguments;

 String application = pathToPdfGenerator + " " + url + " " + outputFullPath;
 String message = runProgram(application); //Note, executed as the application pool user.

 if (message.find("Error") > 0)
   return message.subString(message.find("Error"), 999);
 else
   return "";
}

Then call it by doing something like this:

CreatePDFFromUrl("http://url/to/service", "C:\\temp\\file.pdf", "");
Av: Frode Lillerud 23. okt 2017

RE: Create PDF with CRM Script

Just tried it Frode. Worked like a charm.

Thank you very much !

Av: Ummair Tahir 24. okt 2017

RE: Create PDF with CRM Script

FYI, if you want to create it from a Word file, you can build a small c# service that uses Word interops to convert it for you, and then read the base64 back into CRM/CS.

I used this link to get me started.

https://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically

Av: Pär Pettersson 26. okt 2017