
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.

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
Would like to see a demo…
Wow definitely a nice trick. I didn’t know Google’s PDF viewer worked with external files.
I was hopping to “see-it-in-action”
What does it do exactly?
woops.
Japan’s poor results in google docs.
Using the Docs are wrong?:-(
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.
Looks cool. I’m going to give it a try.
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
Great shortcode indeed
Thanks for sharing !
Hi,
Thanks for sharring this great, and very easy way to do this.
Best regrads
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
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>
===========
Great recipe. Much easier to view the PDF via Google Docs than to open it up with Adobe.
I agree with Gaston. Much faster to open the PDF with Google Docs too.
I love how easy shortcodes are to setup.
Oh sh*t some of code has gone… download it here: http://www.masedi.net/berkas/wp-embedpdf.phps
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?
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: