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) {
      'width' => '800px',
      'height' => '600px'
  ), $attr));
  return '<iframe src="' . $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"][/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