I’m building a WordPress site for a property lettings agent (real estate), which links to their property management system (LetMC).
LetMC provides PDF property brochures through a URL, for example (ignore the brochure content, yet to be designed):
I’ve built a nice system to show property photos, brochure and location using a lightbox through AJAX, and so I don’t want to auto-download the PDF. Currently I’m saving the PDF to a temp folder and opening it direct from the sites web server, but ideally I want to avoid saving the PDF….
Is this possible??
Current progress:
/**
* Lightbox Brochure
*
* Return property brochure to be viewed in a lightbox
*/
public static function lightbox_brochure() {
$letmc_brochure_url = $_REQUEST['dataurl'];
if ( isset( $letmc_brochure_url ) ) {
$filename = generateRandomString() . '.pdf';
$upload_dir = wp_upload_dir();
if ( wp_mkdir_p($upload_dir['basedir']) ) {
$file = $upload_dir['basedir'] . '/letmc_uploads/temp/' . $filename;
}
/**
* Store in the temp filesystem
*/
$ch = curl_init($letmc_brochure_url);
$fp = fopen($file, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$Result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$output = '';
$output .= '<div class="lightbox"><iframe src="' . $upload_dir['baseurl'] . '/letmc_uploads/temp/' . $filename . '" width="97%" height="550"></iframe><button title="Close (Esc)" type="button" class="controls mod-close mfp-close"><i class="icon icon-close"></i></button></div>';
echo $output;
} else {
echo '<p>Sorry, no brochure available.</p>';
}
die();
}
You do not need to save the file to show it later. You can simply access the PDF file and spit it out directly into the iframe as this:
Create file getpdf.php:
Ammend your code as this (place it right below
$output = '';
):$letmc_brochure_url = urlencode($letmc_brochure_url);
$output .= '<div class="lightbox"><iframe src="getpdf.php?url=' . $letmc_brochure_url . '" width="97%" height="550"></iframe><button title="Close (Esc)" type="button" class="controls mod-close mfp-close"><i class="icon icon-close"></i></button></div>';
The only drawback here is that each time the file is requested, it needs to be downloaded again by the server (there’s no caching), which might put some strain on your server resources.
Googleâs Google Docs has a nifty iframe embed tool that allows you to embed and view documents online (pdfâs included)!
Check out the code below. You simply need to supply the url of the document.
You can see a live demo/example of this PDF and Document Online Reader iframe embed tool over at my professional web development and design blog.