Create PDF/Image Download Of A Webpage

Downloadable PDF functionalities are part of different websites. PDF or excel downloads have large number of ways to do. Excel is easy, but PDF usually have issues of CSS and many more.

I have done a lot more research on this and found the usage of CutyCapt.exe more  convenient to capture a webpage as PDF or Image.

What is CutyCapt

CutyCapt is a small cross-platform command-line utility to capture WebKit's rendering of a web page into a variety of vector and bitmap formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, and BMP. See IECapt for a similar tool based on Internet Explorer.

Requirements

  1. The process of PDF creation is done using an executable file called CutyCapt.exe. Download the exe and add it to the solution.
  2. If we are working with ASP.NET project, the exe can be called in a redirection page. This page will be blank with the following code on page load.

Implementation

  1. Create a file name and path: To make the filename unique I would prefer a GUID. (It is the choice of the developer according to the requirement and logic). The file Url is then added to a string.
    1. string filename = Guid.NewGuid() + ".pdf"//unique file name  
    2. // url of the pdf file name   
    3. string url = “http://example.org” +"/CutyCapt/" + filename; 
  2. Location of executable: The location of the CutyCapt.exe is assigned to a string.
    //location of Executable
    string cutycaptLocation = "D:\\CutyCapt";

  3. Command creation: For creating the command in ASP.NET, we use the ProcessStartInfo object.
    Different parameters are assigned to the object as properties like the following:
       a. CreateNoWindow.
       b. WorkingDirectory. 
       c. FileName .
       d. Arguments.
                    - link of the page which needs to be converted to PDF.
                    - the destination folder
       e. UseShellExecute .
    1. // Command for execution   
    2. System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo();  
    3. pi.CreateNoWindow = false;  
    4. pi.WorkingDirectory = cutycaptLocation;  
    5. pi.FileName = cutycaptLocation + "\\CutyCapt.exe";  
    6. pi.Arguments = "--url=" + link + " --out-format=pdf -out=" + System.IO.Path.GetFullPath(Server.MapPath("~/CutyCapt")) + "\\" + filename;  
    7. pi.UseShellExecute = false;
  4. Process execution: Process object in ASP.Net is used to execute the process, for the info created.
    1. try {  
    2.     // Start the process with the info we specified.   
    3.     // Call WaitForExit and then the using statement will close.   
    4.     using(System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(pi)) {  
    5.         exeProcess.WaitForExit();  
    6.     }  
    7. }   
    8. catch (Exception ex)   
    9. {  
    10.     Response.Write(ex.Message);  
    11. }
  5. Redirect to PDF: Once the PDF is created the browser is redirected to the PDF link loading the PDF created.
    1. Response.Redirect(url);  

The PDF file for the specified page is ready to be saved to the system. This is the easiest way to create a PDF in my experience.

Please feel free to put in your comments on this.