Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Forked from mozz100/pdf_magic.js
Created July 27, 2017 00:48
Show Gist options
  • Save bhubbard/a72723eb3aae2ed11c9b5fa0764153dd to your computer and use it in GitHub Desktop.
Save bhubbard/a72723eb3aae2ed11c9b5fa0764153dd to your computer and use it in GitHub Desktop.
Zendesk embedded PDF viewer via Google Docs
// add this javascript to your Zendesk Help Center pages
(function($) {
$(function() {
// for all attachment links whose URLs end with .pdf (case sensitive)...
var pdfs = $('.attachments a[href$=".pdf"]');
var eleBody = $('div.article-body');
if (pdfs.length > 0) {
var msg = '<b>' + pdfs.length + ' ';
msg += 'PDF file';
if (pdfs.length > 1) { msg += 's'; }
msg += ' attached</b> - download ';
if (pdfs.length > 1) { msg += 'them'; } else { msg += 'it' }
msg += ' from the bottom of the page.';
eleBody.append('<p>' + msg + '</p>');
pdfs.each(function(i,o) {
// ...build html to an iframe on google docs viewer
var html = '';
html = '<iframe src="' + window.location.protocol + '//' + 'docs.google.com/viewer?url=';
// url encode the full, absolute URL for the attachment
html += encodeURIComponent(window.location.protocol + '//' + window.location.host + $(o).attr('href'));
// set width and height
html += '&embedded=true" width="100%" height="780" style="border: none;"></iframe>'
// place into main body of the article
eleBody.append(html);
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment