HTML to PDF Syntax

Note : we have rolled out a completely revised API in February 2014. The documentation for that is ON THE MAIN SITE. New users will automatically sign up for that, whilst current API users can choose to work with the old API or switch to the new one. The documentation below applies to paying subscribers to the ‘old’ API only.

If you want to convert HTML code to a PDF you can use the –html function.

Use this if your content (page) is not located online OR if your page is behind a secured login area.

Simply ‘post’ the content to our service like this:

http://pdfmyurl.com?–html=<h1>Hello,</h1><p>This is html content to be pdf’ed</p>

If you have a business license, you can add your domain to the –license parameter, and the pdfmyurl logo will not show up.

http://pdfmyurl.com?–html=<h1>Hello,</h1><p>This is html content to be pdf’ed</p>&–license=example.com

PHP example

The following php code snippet uses this as a principle:

<?php

// load the html file as a string:
$content = file_get_contents('test.html');

// the url that does the work
$url = "http://pdfmyurl.com";

//set the POST variables
$fields = array(
  '--html'=>urlencode($content),
  '--orientation'=>urlencode("portrait"),
  '--licence'=>urlencode("domain.com") /* enter your domain for which you have a license here */ 
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { 
  $fields_string .= $key.'='.$value.'&'; 
}
// get rid of trailing signs
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

// set the file to write to
$fp = fopen('/myfolder/to/save/to/test.pdf', 'wb');

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);

// the file to put the results in
curl_setopt($ch, CURLOPT_FILE, $fp);

// the count the number of fields to post
curl_setopt($ch, CURLOPT_POST, count($fields));

// set up the fields to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post: and the work
$result = curl_exec($ch);

//close connection
curl_close($ch);

// returns 1 if success 0 if failed
echo $result;

?>

The php code is a proof of concept only, if you or someone you know is fluent in any other language, you should be able to translate the php code with reasonable effort.

If you have a business license, you can add your domain to the –license parameter, and the pdfmyurl logo will not show up.

Note: you must use pdfmyurl.com as the $url that does the work, this will not work for the domain www.pdfmyurl.com, or you will get a “301 Moved Permanently” header status response.

The basic flow is the same, if your use-case is the same:

  1. load the html file to be converted in a variable
  2. post the variable containing the html to our server, along with any other options
  3. get the pdf’ed version back and save it on your side for further processing

ASP example

The following asp code snippet shows how you can provide a link to pdf’ed content:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDFGenerator._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <style type="text/css">
  .errorMessage {
    color: red;
    font-family: Verdana;
    font-size: small;
  }
  </style>
</head>
<body>
    <form id="aForm" runat="server">
        <asp:Label ID="labelErrorMessage" runat="server" CssClass="errorMessage" EnableViewState="false"></asp:Label>
        <br />
        <asp:HyperLink ID="pdfLink" runat="server">Get PDF</asp:HyperLink>
    </form>
</body>
</html>

We can manipulate the HyperLink control in the Page_Load event which is triggered when a page loads:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;
using System.Text;
using System.Net;

namespace PDFGenerator
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // this is where the html file is at:
            string path = Server.MapPath(@"html\convertMe.htm");

            // Check if the file is there.
            if (File.Exists(path))
            {
                // The following shows how to generate the link (url) that when clicked is pdf'ed
                string theHtmlToConvert = Server.UrlEncode( File.ReadAllText(path) );
                string URL = "http://pdfmyurl.com?--html=" + theHtmlToConvert;
                pdfLink.NavigateUrl = URL;

                // The following is commented. Uncomment to download the pdf to the web server
                //string DestinationPath = "C:\\AllThePdfs\\example.pdf";
                //System.Net.WebClient Client = new WebClient();
                //Client.DownloadFile(URL, DestinationPath); 

            }
            else
            {
                labelErrorMessage.Text = "Unable to locate the html file for conversion.";
            }
        }
    }
}

Running this example, the user now has a link which sends the html file to the pdfmyurl server (“html\convertME.htm”).

Also take a look at the commented example code, which shows how you can download the pdf to the web server.

Leave a Reply

Your email address will not be published. Required fields are marked *