Knowledgebase: Pro Features
Using the URL-to-PDF Conversion PRO Feature
Posted by Duane Roumel on 10 May 2013 07:20 PM

The URL-to-PDF Conversion PRO Feature is used to convert a URL to PDF file format.

This feature is based on the most advanced and stable PDF conversion open-source resource. However, in many cases, the formatting inside the PDF does not match the website. This is due to limitations of the rendering engine for PDF. As the community improves this utility, we will update our integration of it in order to support more websites.

On occasion, we have assisted customers who needed to provide a more accurate rendering of a URL in a PDF. We achieved this by way of converting a regular screenshot into a PDF, using ImageMagick's convert utility. The simplest example would be:

convert test.jpg test.pdf

Note: As of 01/29/2016, ShrinkTheWeb PDF Capture does NOT support Flash
Note: May not work with sites that require Javascript and/or Cookies enabled

Simple Method

Start with the Sample BASIC Code: 

http://images.shrinktheweb.com/xino.php?stwembed=1&stwaccesskeyid=&stwurl=http://domain.com

Note: PRO features have no effect on PDF captures at this time. This feature will output a PDF with as many pages as it takes to capture the Full-Length of the web page.

• Add &stwpdf=1
(Embedded will open a download dialog)
(Advanced Method will download the PDF immediately)

Emample:

 http://images.shrinktheweb.com/xino.php?stwembed=1&stwpdf=1&stwaccesskeyid=&stwurl=http://www.yoursitehere.com/invoice1012.html

 

Advanced Method (API Requests)

Here is some sample PHP code to download the PDF and store it locally:

// As of 01/29/2016, ShrinkTheWeb PDF Capture does NOT support Flash
// May not work with sites that require Javascript and/or Cookies enabled
// This feature is based on an open-source PDF convertor that sometimes fails to properly format content from the website into the PDF
$Url = 'http://www.ferrari.com'; // pull from database
$sFileUrl = 'http://images.shrinktheweb.com/xino.php';
$sParams = '?stwu=[STW_SECRET_KEY]&stwaccesskeyid=[STW_ACCESS_KEY]&stwpdf=1&stwurl=' . $Url;

// Use the URL for the basis of the PDF filename to save
// Delete this section if using a hash or other convention
if (preg_match ('/^http[s]?+:\/\//', strtolower($Url), $schemeMatches)){
     $scheme_ref = $schemeMatches[0]; // Pull out the scheme for later use
}
$tld = str_ireplace($scheme_ref, '', $Url); // Strip off scheme to work with rest of URL
$tld_www = array_shift (explode ('/', $tld)); // this is a safer domain.tld test (allows WWW)
// End of formatting URL as [www.]domain.tld
$pdffile = $tld_www . '.pdf'; // Create your own naming convention to meet your needs

if(function_exists('curl_init')) {
     $rConnect = curl_init();

     curl_setopt($rConnect, CURLOPT_URL, $sFileUrl . $sParams);
     curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1);

     $sResult = curl_exec($rConnect);
     curl_close($rConnect);
}
else {
     // If CURL is not available; Please note that allow_url_fopen must be enabled in php.ini for this call to work
     $sResult = file_get_contents($sFileUrl . $sParams, 'rb');
}

if($sResult) {
     $fp = fopen($pdffile, 'w+');
     if($fp) {
          fwrite($fp, $sResult);
          fclose($fp);
     }
}

 

Which PDF Conversion Method Should I Choose?

The method you should choose depends on how you want to use the PDF feature. The "Simple" ("Embedded") method will display the PDF in the user's browser (IF they have adobe PDF support in their browser) or will prompt them to download, if not. The other method is the advanced method (notice that the SECRET KEY is required). The advanced method will always prompt for a download, if made into a clickable link, because it will send the raw PDF data. Since you do NOT want to expose your secret key, this should link to a middleman script (like our sample code) that calls the service and returns the PDF.

However, if the page you want to convert to PDF will not change often, then your user's experience will be greatly improved if you use the advanced method behind-the-scenes and download/cache the PDF every once in awhile (linking to the cached version). This is because every user who clicks the link will have to wait, while we convert to PDF in real-time, which could take anywhere between 5 seconds to 90 seconds, depending on how slow-loading the URL is. So, if you save it locally, the users would get the PDF instantly, and you could just update it occasionally. This would also save "New Requests" and "Bandwidth" costs for your ShrinkTheWeb usage limits.

 

3rd Party Plugins

This is a new feature and may not yet be available in the ShrinkTheWeb Certified Plugins.  Please review the plugin documentation for usage.  

(0 vote(s))
Helpful
Not helpful

Contact Us | PagePix Benefits | Learn More | STW Forums | Our Partners | Privacy Policy | Terms of Use

©2016 ShrinkTheWeb. All rights reserved. ShrinkTheWeb is a registered trademark of ShrinkTheWeb.