Open PDF files in the browser

  • Hey Josh,

    I typed this in as the URL mysite.com/.../pdf.js and got the save/cancel dialog box, so I assume this is good.

    The "open pdf browser" does not appear in the document dropdown.

    I went into SPD and reset the v4.master to its original.

    I resaved the code above as pdf.js, verified the paste in v4.master (resaved).  

    It showed the following erorrs when I saved <script type="text/javascript" src="/utilities/pdf.js"></script>

    Found 49 Problems

    Line 198: All attribute values must be surrounded by quotation marks in XHTML.

    Line 199: All attribute values must be surrounded by quotation marks in XHTML.

    Line 200: All attribute values must be surrounded by quotation marks in XHTML.

    Line 201: All attribute values must be surrounded by quotation marks in XHTML.

    Line 218: All attribute values must be surrounded by quotation marks in XHTML.

    Line 219: All attribute values must be surrounded by quotation marks in XHTML.

    Line 220: All attribute values must be surrounded by quotation marks in XHTML.

    Line 221: All attribute values must be surrounded by quotation marks in XHTML.

    Line 223: All attribute values must be surrounded by quotation marks in XHTML.

    Line 224: All attribute values must be surrounded by quotation marks in XHTML.

    Line 225: All attribute values must be surrounded by quotation marks in XHTML.

    Line 226: All attribute values must be surrounded by quotation marks in XHTML.

    Line 262: All attribute values must be surrounded by quotation marks in XHTML.

    Line 263: All attribute values must be surrounded by quotation marks in XHTML.

    Line 264: All attribute values must be surrounded by quotation marks in XHTML.

    Line 265: All attribute values must be surrounded by quotation marks in XHTML.

    Line 563: All attribute values must be surrounded by quotation marks in XHTML.

    Line 564: All attribute values must be surrounded by quotation marks in XHTML.

    Line 565: All attribute values must be surrounded by quotation marks in XHTML.

    Line 566: All attribute values must be surrounded by quotation marks in XHTML.

    Line 580: All attribute values must be surrounded by quotation marks in XHTML.

    Line 581: All attribute values must be surrounded by quotation marks in XHTML.

    Line 582: All attribute values must be surrounded by quotation marks in XHTML.

    Line 583: All attribute values must be surrounded by quotation marks in XHTML.

    Line 10: In XHTML 1.0 Strict the attribute '__expr-val-dir' is not permitted for the <html> tag.

    Line 335: In XHTML 1.0 Strict the attribute 'align' is not permitted for the <img> tag.

    Line 335: In XHTML 1.0 Strict the attribute 'border' is not permitted for the <img> tag.

    Line 454: In XHTML 1.0 Strict the attribute 'nowrap' is not permitted for the <td> tag.

    Line 546: In XHTML 1.0 Strict the attribute 'nowrap' is not permitted for the <td> tag.

    Line 471: In XHTML 1.0 Strict the attribute 'onscroll' is not permitted for the <SharePoint:SPRememberScroll> tag.

    Line 513: In XHTML 1.0 Strict the attribute 'onscroll' is not permitted for the <SharePoint:SPRememberScroll> tag.

    Line 36: In XHTML 1.0 Strict the attribute 'scroll' is not permitted for the <body> tag.

    Line 316: In XHTML 1.0 Strict the attribute 'tabindex' is not permitted for the <div> tag.

    Line 389: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 395: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 418: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 436: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 443: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 496: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 540: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 555: In XHTML 1.0 Strict the tag <ContentTemplate> is not permitted.

    Line 57: In XHTML 1.0 Strict the tag <form> cannot contain a tag <a>.

    Line 636: In XHTML 1.0 Strict the tag <form> cannot contain a tag <input>.

    Line 637: In XHTML 1.0 Strict the tag <form> cannot contain a tag <input>.

    Line 642: In XHTML 1.0 Strict the tag <html> cannot contain a tag <script>.

    Line 614: In XHTML 1.0 Strict the tag <wssuc:DesignModeConsole> is not permitted.

    Line 248: In XHTML 1.0 Strict the tag <wssuc:Welcome> is not permitted.

    Line 549: The <tbody> tag was not closed before this </table> tag.

    Line 551: The <tbody> tag was not closed before this </table> tag.

    Not sure where to go from here.

    Mark

  • Hi Mark,

    All those errors are regarding the sharepoint HTML so don't worry about them.

    It looks like you may have your utilities library on your teamsite instead of on the top level site which for P1 is the public website.  I'd recommend adding the Utilities library to your top level site so it can be used on both the public site and the teamsite.  

    You have two options:

    1) Create utilities Library on your website and upload pdf.js to there

        to do this go here and click 'create'

        http://<yoursite>.sharepoint.com/_layouts/viewlsts.aspx

    2) Leave utilities on the teamsite and change script reference in v4.master to this:

    <script type="text/javascript" src="/teamsite/utilities/pdf.js"></script>

     

    #1 will allow PDF in browser from both  public site(when authenticated), and teamsite plus all subsites below teamsite

    #2 will allow PDF in browser from only teamsite and subsites below teamsite.

    Please let me know if you have questions.

    Thanks,

    Josh

  • Josh, I created the library "utilities" at the Website/root level and uploaded the files.

    I reset all of my v4master's to the original and then inserted the script into the v4master at the root site.

    I loaded a PDF in my root site and it did load in the browser.  I tried in my team site and it did not load.  I then modified my v4master in my team site and it worked in my document sites.  I was not sure if it was supposed to be pervasive once I loaded in the root.

    I do not see the PDF logo.  Was I supposed to see that from the earier step from Vamsi?

    1) Step 1:Download pdf icon and name it as ICPDF.gif

    www.iconarchive.com/.../Adobe-PDF-Document-icon.html

    Thank you for all of your help.  I really appreciate it.

    Mark

  • Hi Mark,

    Glad it works for you. I though the teamsite inherits ther website v4.master, apparently not.

    I think something changed in SPO that broke the PDF icon.  MS finaly added their own icon for PDF files that looks like a belt buckle.  I could mess with the script to bet it to display the adobe icon.  When I get to that I will post here.

    Josh

  • jbooker,

    Thank you for posting this, it was a great help.  I am wondering if you have any further ideas on documents that are grouped.  When I expand a group the first time the documents only prompt me for a save, when I collapse and re-expand then the documents open like I hoped they would.  I also found that when using the search and clicking on the document that I want to view the window opens but it remains blank.  Any help you can give is greatly appreciated.

    Thank you for your effort on this.

    Chip

  • Hi CSjule,

    Glad you like it.

    That's an odd thing that I spent a lot of time on way back when and couldn't resolve.  It has to do with this part of the script:

    //Optional Grouped View Fix

    // Override ExpGroupRenderData in init.js to call Page_Load() upon expand group

    ExpGroupRenderData = (function (old) {

       return function (htmlToRender, groupName, isLoaded) {

       var result = old(htmlToRender, groupName, isLoaded);

    Page_Load();

    };

    })(ExpGroupRenderData);

    ExpCollGroup = (function (old) {

       return function (c,h,m,l) {

       var result = old(c,h,m,l);

    Page_Load();

    };

    })(ExpCollGroup);

    //End Override

    ExpGroupRenderData() and ExpCollGroup() are javascript functions in the sharepoint built-in init.js file.  They are responsible for rendering the hidden items when you expand or collapse a grouped view.  My script overrides these built-in functions so I can call my function after they execute.  The idea is to apply the PDF enhancements after the expanded items are rendered.  Basically my code is firing, but the items are not rendered on the page when it fires the first time.  Subsequent expands call my code again at which point the items are there so the enhancements get applied successfully.

    That's all I know as I ran out of time to troubleshoot this further.  My guess is there may be a way to get it to work on the first expand, but I won't be working on this anytime soon.  Perhaps some one crafty will find this and pick up where I left off.

    Have a great day.

    Josh

  • Josh thank you for your response. I also ran into an issue when using the search and clicking on the document that I want to view the window opens but it remains blank.  Do you have any ideas on that?

    Thank you,

    Chip

  • I altered the script above to use the href from the page rather than to append the protocol & host.  This ensures the script will work on search pages which have absolute URLs instead of relative ones.  Please copy the new script above or simply replace yours with this one:

     

    http://joshuabooker.com/Documents/pdf.js

     

    Let me know if that works for you.

     

    Josh

  • That works great, thank you.  I will continue to work on the Group View Fix and if I find anything I will let you know.

    Thanks again,

    Chip

  • Hello,

    I found that when going to the site permissions page I was getting a object expected alert and I remedied it by adding a couple of $items != null.  Do you have any ideas on this approach?

    try{//assign javascript for the pdf files

       //var $items = $('.ms-vb a[href$=".pdf"]');

       var $items = $('a[href$=".pdf"]');  //removed .ms-vb class selector to catch public site links and app page links

       if ( $items != null)

    {

        $items.each(function(index)

    {//var $itemURL =  document.location.protocol + "//" +

    //document.location.host + $(this).attr('href');

    var $itemURL =  $(this).attr('href');

    $(this).removeAttr("onclick");

    $(this).unbind("click");

    $(this).attr(

    { onclick: 'javascript:OpenPDF("' + $itemURL + '"); return false',

    onmousedown: "return VerifyHref(this,event,'0','','')"

    });   //return false added so href is not followed when open new window - quote marks swapped to handle file names with apostrophe                      

    });

            }

    }

    catch (e)

    {

         if ( $items != null)

              {alert(e.message);}

    }

    Thanks,

    Chip

  • Hi Josh,

    I am wondering if you have any ideas about another issue I have come up against.  If I send out a link to a .pdf document in SharePoint to another user and they try to click on that link it only gives them the save or cancel options.  If you click on the document on either the search page or from the library within SharePoint it works great.  Any information is greatly appreciated.

    Thanks,

    Chip

  • Hi Chip,

    The $items != null thing is nice if that fixes your problem.  Most likely it's the one in the catch that stops the alert because the upper loop only executes if there are elements in $items collection.

     

    The solution uses javascript to change the code on the site that opens a PDF file, so sending direct links will not work.  If you implement my other solution below, then you can send links like so:

    http://yoursite.sharepoint.com/utilities/pdf.aspx?file=http://yoursite.sharepoint.com/yourdoclibrary/yourPDFfile.pdf

    That link will work from email, but it will not be the link that comes from 'Email a Link' button in sharepoint.  You'll have to Edit hyperlink in the email and paste the:

    http://yoursite.sharepoint.com/utilities/pdf.aspx?file=

    infront of the PDF url.  

     

    Solution:

    http://joshuabooker.com/Pages/PDF-CompleteSolution.aspx

     

    PS...Since you have the other script in place, you only have to do the 'Get files on your environment:' part of the above solution.  And only upload the PDF.aspx file.

     

    Josh

    1 out of 1 people found this post helpful.

  • Josh,

    Thank you for your reply, it works great.  This post has been a lot of help.  Thank you for sharing your ideas, I am new to SharePoint and this has been a great learning opportunity.

    Chip

  • Hi Josh,

    I am having an issue with some users not getting the document to open in the browser.  It works for about 75% of the users but I have come up empty handed on the other 25%.  They are only getting the option save or cancel.  Do you have any ideas as to what the issue could possibly be?  I even had one user long on to a PC where it works for me but does not work for her.  Any help is greatly appreciated.

    Thank you,

    Chip

  • Hi Chip,

    The solution requires adobe reader browser add-in installed.  But if they can open PDFs in the browser from other sites then they should be able to here.  MS made some changes with service update 2 which allow opening PDFs in Adobe locally (not in browser).  You need  a certain version of adobe X installed for MS thing to work.  With MS solution, different adobe versions is the most common reason for some PCs woking and others not, but that shouldn't affect this script.

    I cannot think why it would work for one user but not another on the same PC.  Unless the user doesn't have permissions to Utilities library.

    Are you testing with exact same PDF file in same library?  

    Let me know if you have any other clues.

    Josh