Link Search Menu Expand Document

Convert HTML to PDF - SharePoint

PDF from HTML (HTML to PDF) sample in SharePoint demonstrating ‘Convert HTML to PDF’

Utils.cs
namespace ConvertWebPageToPDFLinkWebPart.VisualWebPart1
{
    public class Utils
    {
        public static string API_KEY = "--ADD-YOUR-PDF-CO-API-KEY-HERE--";
    }
}

VisualWebPart1.cs
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ConvertWebPageToPDFLinkWebPart.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/ConvertWebPageToPDFLinkWebPart/VisualWebPart1/VisualWebPart1UserControl.ascx";

        protected override void CreateChildControls()
        {
            var control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
            Controls.Add(control);

            control.CurrentWeb = SPContext.Current.Web;
        }
    }
}

VisualWebPart1UserControl.ascx.cs
using Microsoft.SharePoint;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ConvertWebPageToPDFLinkWebPart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        public SPWeb CurrentWeb { get; set; }

        // The authentication key (API Key).
        // Get your own by registering at https://app.pdf.co
        string API_KEY = Utils.API_KEY;

        // Destination PDF file name
        const string DestinationFile = "result.pdf";
        const string DestinationLibName = "Shared Documents";

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void StartButton_Click(object sender, EventArgs e)
        {
            // Create standard .NET web client instance
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebClient webClient = new WebClient();
            var SourceUrl = UrlTextBox.Text;

            if (String.IsNullOrWhiteSpace(SourceUrl))
            {
                LogTextBox.Text += "Enter valid web page url \n";
                return;
            }

            // Set API Key
            webClient.Headers.Add("x-api-key", API_KEY);
            
            // URL for `Web Page to PDF` API call
            string url = "https://api.pdf.co/v1/pdf/convert/from/url";

            // Prepare requests params as JSON
            Dictionary<string, object> requestBody = new Dictionary<string, object>();
            requestBody.Add("name", DestinationFile);
            requestBody.Add("url", SourceUrl);

            // Convert dictionary of params to JSON
            string jsonPayload = JsonConvert.SerializeObject(requestBody);

            try
            {
                // Execute POST request
                var response = webClient.UploadString(url, "POST", jsonPayload);

                // Parse JSON response
                JObject json = JObject.Parse(response);

                if (json["error"].ToObject<bool>() == false)
                {
                    // Get URL of generated PDF file
                    string resultFileUrl = json["url"].ToString();

                    // Download PDF file
                    var retData = webClient.DownloadData(resultFileUrl);

                    //Upload file to SharePoint document library
                    //Read create stream
                    using (MemoryStream stream = new MemoryStream(retData))
                    {
                        //Get handle of library
                        SPFolder spLibrary = CurrentWeb.Folders[DestinationLibName];

                        //Replace existing file
                        var replaceExistingFile = true;

                        //Upload document to library
                        SPFile spfile = spLibrary.Files.Add(DestinationFile, stream, replaceExistingFile);
                        spLibrary.Update();
                    }

                    LogTextBox.Text += String.Format("Generated PDF document saved as \"{0}\\{1}\" file. \n", DestinationLibName, DestinationFile);
                }
                else
                {
                    LogTextBox.Text += json["message"].ToString() + " \n";
                }
            }
            catch (Exception ex)
            {
                LogTextBox.Text += ex.ToString() + " \n";
            }

            webClient.Dispose();

            LogTextBox.Text += "\n";
            LogTextBox.Text += "Done...\n";
        }
    }
}

VisualWebPart1UserControl.ascx.designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConvertWebPageToPDFLinkWebPart.VisualWebPart1
{


    public partial class VisualWebPart1UserControl
    {

        /// <summary>
        /// UrlTextBox control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox UrlTextBox;

        /// <summary>
        /// StartButton control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button StartButton;

        /// <summary>
        /// LogTextBox control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox LogTextBox;
    }
}

PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

Get your PDF.co API key here!

Download Source Code (.zip)

return to the previous page explore PDF from HTML (HTML to PDF) endpoint