1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating an add-on called Open PDF

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 24, 2013.

  1. AndyB

    AndyB Well-Known Member

    Description:

    When a PDF file is attached to a post, clicking the attachment link will open a dialog box asking where to save the file. This Add-on changes this behavior so the PDF is opened in the browser.

    Requirements:

    This add-on only works on Xenforo v1.2 and above.
     
    0ptima, JulianD and Amaury like this.
  2. AndyB

    AndyB Well-Known Member

    Create the new add-on:

    Admin CP -> List Add-ons -> Create Add-on

    pic001.jpg
     
    Pereira and Amaury like this.
  3. AndyB

    AndyB Well-Known Member

    Create the Code Event Listener:

    pic002.jpg
     
    Amaury likes this.
  4. AndyB

    AndyB Well-Known Member

    Create the add-on directories and files:

    library
    --Andy
    ----OpenPDF
    ------Lisener.php
    ------ViewPublic
    --------Attachment
    ----------View.php
     
    Amaury likes this.
  5. AndyB

    AndyB Well-Known Member

    The Listener.php file:

    Create the /library/Andy/OpenPDF/Listener.php file.

    PHP:
    <?php

    class Andy_OpenPDF_Listener
    {
       public static function 
    View($class, array &$extend)
       {
         
    $extend[] = 'Andy_OpenPDF_ViewPublic_Attachment_View';
       }
    }

    ?>
     
    Amaury likes this.
  6. AndyB

    AndyB Well-Known Member

    The View.php file:

    Create the /library/Andy/OpenPDF/ViewPublic/Attachment/View.php file.

    PHP:
    <?php

    class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
    {
       
    //######################################## 
       // the original renderRaw() function is located in library/Xenforo/ViewPublic/Attachment/View.php
       // this renderRaw() will run instead of the original unless we the return parent::renderRaw()
       //########################################

       
    public function renderRaw()
       { 
         
    $attachment $this->_params['attachment'];
         
    $extension XenForo_Helper_File::getFileExtension($attachment['filename']);
     
         if (
    $extension === 'pdf')
         {
           
    $this->_response->setHeader('Content-type''application/pdf'true);
           
    $this->setDownloadFileName($attachment['filename'], true);
       
           
    $this->_response->setHeader('ETag'$attachment['attach_date'], true);
           
    $this->_response->setHeader('Content-Length'$attachment['file_size'], true);
           
    $this->_response->setHeader('X-Content-Type-Options''nosniff');     
       
           return new 
    XenForo_FileOutput($this->_params['attachmentFile']);
         } else {
           
    // run the original renderRaw() function
           
    return parent::renderRaw();
         }
       }
    }
    ?>

     
    Amaury likes this.
  7. AndyB

    AndyB Well-Known Member

    The Add-on is now complete:

    Click on a PDF attachment and it should open in the browser.
     
    Amaury likes this.
  8. Alfa1

    Alfa1 Well-Known Member

    Have you considered using a more advanced PDF viewer? I find that browsers like Firefox display PDF's really bad.
    Plus nowadays there are various very nice document viewers (jQuery, flash or HTM5) with flipbook effect, full screen view and other options. FlexPaper is a free solution.
     
    Last edited: Sep 24, 2013
  9. AndyB

    AndyB Well-Known Member

    Hi Alfa1,

    I agree, the PDF reader that now ships with Firefox is terrible. Thankfully Mac's come with Preview and it works very well to read PDF files. I have set the preference in Firefox to use Preview.
     
  10. Alfa1

    Alfa1 Well-Known Member

    Unfortunately most people are not power users, they will use firefox's PDF reader.
     
  11. AndyB

    AndyB Well-Known Member

    I was better when Firefox didn't have a PDF reader built in so the PDF would be read with either Adobe Reader or Preview.
     
    haikhongkhong and Alfa1 like this.
  12. 0ptima

    0ptima Well-Known Member

    Thanks for posting this.
     

Share This Page