Create An Easy PDF Preview for Search Results in SharePoint 2013

I saw this question flying around the other day and thought it was kind of interesting.  Someone was asking about the cool new thumbnail previews you get for items in search results in SharePoint 2013.  They really liked how Microsoft Office documents work and wanted to be able to do the same thing with PDF files.  There was an answer, which is a good one, that you could create a custom WOPI extension that would render PDF files using Office Web Apps.  The drawback to that is that it requires someone to do some coding and testing (using full trust code), then deploy it to your OWA farm, then create a new WOPI binding in the SharePoint farm.  Again, an entirely reasonable response if your requirements dictate that.

I, however, representing all that is good about being lazy and cheap, decided to take a different approach to this.  Without digging into the particulars of how all the plumbing works, at the end of the day a search results preview for Office documents are just done via an iFrame.  So why not do the same thing for PDF documents?  Why not indeed – that’s exactly what I did.  Here’s all I did to make this work:

  1. I went to my search center site and download a copy of the Item_PDF.html, which is the out ofthe box display template for a PDF item in SharePoint 2013.  I made the following changes to it:
    1. Changed the Title to “PDF by Steve”
    2. Changed the hoverUrl variable in the javascript to “~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Site_HoverPanel.js”
  2. I saved it as PDF_Default.html and went back to my SharePoint search center site, then navigated to Site Settings…Master pages and page layouts…Display Templates…Search and uploaded it.
  3. I went back to Site Settings and selected Search Result Types in the Site Collection Administration section.
  4. I created a Search Result Type and configured it as follows:
    1. Name:  PDF Viewer
    2. What types of content should match? :  PDF
    3. What should these results look like?  :  PDF by Steve

That’s it – now you’re ready to go.  One thing to point out here – in step 1b I configured the display template to use the hover panel that comes out of the box for sites.  It is configured to use an iFrame and render it nice and pretty, so I didn’t even have to create a hover panel for it.  As a side note I actually DID do that when I was fooling around, but the out of the box stuff looks so much better than what I can do I figured why fight it – graphical beauty ain’t my thing.  Once it’s done I get a nice preview for my PDF documents in search results, which you can see here:

That’s all there is too it.  I’ve attached my display template here, just in case you are as lazy and cheap as I am.  :-)   Hope everyone has a Merry Christmas and Happy New Year.

UPDATE:  I did discover one big limitation with this approach – it only works if your PDF documents are in the same host as your search center, i.e. if they are in the same web application is the easiest example.  So if you have your search center at http://www.foo.com, but your PDFs are in http://www.bar.com then they will not be displayed as preview items.  That’s because of some X-Frame-Options headers that we send down with SharePoint now. If you are following our new recommendations for SharePoint 2013 to use a single web application for your farm then you will be fine, but if you have multiple web applications or are crawling content from non-SharePoint sites then it will not render in the preview.

 

Couple other possible gotchas to be aware of – 1) make sure you have installed the Adobe reader on the machine on which you are viewing the previews and 2) I’ve got at least one report saying this doesn’t work in a non Internet Explorer browser.

 

You can download the attachment here:

3 thoughts on “Create An Easy PDF Preview for Search Results in SharePoint 2013

  1. Hello Steve,

    Thank you for this great post!

    I have Trouble with the new Approach “One Farm, One Webapplication” and HNSCs together with the search previews.

    My Setup in short:

    1 SSL Webapplication with SC (Root, for the Search Service). No User hast Access to it.
    3 SSL HNSC (Intranet.contoso.com, Team.contoso.com, My.contoso.com)

    Where do I place my Search Center, so that previews from all adresses are rendered? Currently my Search Center is in Team, but then I got no previews for Intranet.contoso and my.contoso!

    I really Need to get the previews going with this new advertised “1 Farm, 1 Webapp, Lot of HNSCs” Approach!

    Regards!

    Like

  2. Hi,

    The file attached to the article – “PDF_Default.html” appears to be the .NET runtime error screen instead of an actual display template. Is this correct?

    Like

  3. Hi
    Thanks a lot for the solution. It worked but only for IE.
    Issues :
    1.In Chrome the height and the width for the preview content is very less
    2. in Firefox width and height is OK but scroll is not working.

    Do you have any solution for this ?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s