Google Docs PDF viewer WordPress shortcode

Sometimes it’s nice to display a PDF embedded on a page without forcing the visitor to download it. Using the Google Docs PDF viewer, we can easily do that with a shortcode and this snippet of code.

function alienship_display_pdf($attr, $url) {
  extract(shortcode_atts(array(
      'width' => '800px',
      'height' => '600px'
  ), $attr));
  return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$width. '; height:' .$height. ';">Your browser does not support iframes</iframe>';
}
add_shortcode('pdf', 'alienship_display_pdf');

To display the PDF, insert the shortcode in your page or post using the following syntax:

[pdf width="550px" height="420px"]http://yoursite.com/filename.pdf[/pdf]

If you do not specify a width and height, the default size will be 800×600 pixels using the code above.

Leave a Reply