Post Pic

WordPress tip: Create a PDF viewer shortcode

If you’re using PDF files on your WordPress blog, it could be very cool to give your users the chance to open them using Google Docs. The following recipe will show you how you can do that.

The first step is to paste the following code into your functions.php file:

function pdflink($attr, $content) {
	return '<a class="pdf" href="http://docs.google.com/viewer?url=' . $attr['href'] . '">'.$content.'</a>';
}
add_shortcode('pdf', 'pdflink');

Once you saved the file, you'll be able to use the shortcode on your posts and page. Here is the syntax:

[pdf href="http://yoursite.com/linktoyour/file.pdf"]View PDF[/pdf]

Thanks to Noscope for this great shortcode!

20 Responses

Mar 01 2010 08:38

Would like to see a demo…

Mar 01 2010 08:42

Wow definitely a nice trick. I didn’t know Google’s PDF viewer worked with external files.

Mar 01 2010 09:36

I was hopping to “see-it-in-action” :)

Mar 01 2010 10:02

What does it do exactly?

Mar 01 2010 11:20

woops.
Japan’s poor results in google docs.
Using the Docs are wrong?:-(

Mar 01 2010 13:38

Thanks for the link-love, Recipes!

For those of you who want to see a link in action, here’s one:

http://docs.google.com/viewer?url=http://noscope.com/media/no.pdf

It’s my logo, saved as a PDF, uploaded, and wrapped as a Google Viewer link.

Mar 01 2010 18:36

Looks cool. I’m going to give it a try.

Mar 02 2010 04:22

Hi,This is great.i was searching for this.i added to my site tutorial section and was successful.but when i opened to view.pdf of that tutorial i found that it says Sorry,we are unable to retrieve.please try again later.
also i found that download shows the size of file.how to solve it.please reply

Mar 02 2010 05:19

Great shortcode indeed

Thanks for sharing !

Mar 07 2010 17:37

Hi,

Thanks for sharring this great, and very easy way to do this.

Best regrads

Mar 09 2010 04:22

Thanks for the great tip. I have tried it in one of my WP membership sites and it works well.

Also, I have added a few tweaks to the code that goes into the functions.php file within the blog’s theme folder to make it so that the Google Docs PDF preview opens in a new separate window when you click the shortcode link by adding the very simple HTML tag

target=”_blank”

as shown below between the two lines.

=========
function pdflink($attr, $content) {
return ‘‘.$content.’‘;
}
add_shortcode(‘pdf’, ‘pdflink’);
=========

One final point, if you want to customize the font style and size of the PDF link within your post, you can do so when viewing the shortcode in HTML mode as follows:

=========
[pdf href="http://www.example.com/blog/wp-content/uploads/example.pdf"]

View PDF

[/pdf]
=========

In the above case the link would appear in the blog post with the Verdana font (size = 12pt).

Hope this helps!

Regards,
Graeme Thom

Mar 09 2010 04:37

Woops – forgot that the code would become invisible. Let me try again!

First code block above:

===========
function pdflink($attr, $content) {
return ‘<a class="pdf" href="http://docs.google.com/viewer?url=’ . $attr['href'] . ‘" target="_blank">’.$content.’</a>’;
}
add_shortcode(‘pdf’, ‘pdflink’);
===========

Second code block above:

===========
<pre>
[pdf href="http://www.example.com/blog/wp-content/uploads/example.pdf"]
<div style="font-family: Verdana; font-size: 12px;">
View PDF
</div>
[/pdf]
</pre>
===========

Mar 15 2010 12:14

Great recipe. Much easier to view the PDF via Google Docs than to open it up with Adobe.

Mar 17 2010 06:37

I agree with Gaston. Much faster to open the PDF with Google Docs too.

I love how easy shortcodes are to setup.

Apr 01 2010 20:10

Oh sh*t some of code has gone… download it here: http://www.masedi.net/berkas/wp-embedpdf.phps

May 10 2010 09:09

Great post! Thanks a lot… but I also had a question in my mind about PDFs.
Ive seen websites that they automatically generate a PDF version of the specific post and there is a button in every post that allows people to download the pdf version of the post… is there any hacks or plugins for that?

Sep 04 2010 12:03

Forgive me if this is missing something obvious, but Safari (on Mac – don’t know about Windows) opens PDF’s in the browser anyway.

Maybe modify the shortcode to exclude browsers which handle PDF’s properly?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required