Darussalam Posted March 2, 2016 Share Posted March 2, 2016 Hello, I have searched a lot and tried many things but can't get the PDF attachments on the product page to open directly instead of showing the download window. I have made the changes to the AttachmentController.php in the controllers > front folder as mentioned in this old post: https://www.prestashop.com/forums/topic/252894-solved-open-pdf-in-new-window/ but that solution doesn't seem to work on the latest version of Prestashop 1.6.1.4. The only browser that it is working correctly is in Microsoft Edge. The problem occurs in both Chrome and Firefox. Here is a page from my site where you can see the download link: http://dar-us-salam.com/english-books/dawah-new-non-muslims/what-is-islam.html Google Chrome Console shows this error: Resource interpreted as Document but transferred with MIME type application/octet-stream. I have added AddType application/pdf .pdf to the htaccess file but that didn't help. Any help will be greatly appreciated. Let me know if any further information is needed. Thanks Omar http://dar-us-salam.com Link to comment Share on other sites More sharing options...
Darussalam Posted March 2, 2016 Author Share Posted March 2, 2016 Here is my AttachmentController.php file. When I comment out this line (as mentioned in another post): header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"'); all together, both Firefox and Chrome try to download an index.php file so I know something is wrong. class AttachmentControllerCore extends FrontController { public function postProcess() { $a = new Attachment(Tools::getValue('id_attachment'), $this->context->language->id); if (!$a->id) { Tools::redirect('index.php'); } Hook::exec('actionDownloadAttachment', array('attachment' => &$a)); if (ob_get_level() && ob_get_length() > 0) { ob_end_clean(); } header('Content-Transfer-Encoding: binary'); header('Content-Type: '.$a->mime); header('Content-Length: '.filesize(_PS_DOWNLOAD_DIR_.$a->file)); /* Should open for viewing */ header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"'); /* original code */ /* header('Content-Disposition: attachment; filename="'.utf8_decode($a->file_name).'"'); */ @set_time_limit(0); readfile(_PS_DOWNLOAD_DIR_.$a->file); exit; } } Link to comment Share on other sites More sharing options...
SupportPS Posted March 2, 2016 Share Posted March 2, 2016 Hello. PDF files will only open when the browser add-ons installed to read. Link to comment Share on other sites More sharing options...
Darussalam Posted March 2, 2016 Author Share Posted March 2, 2016 Yes, browser add-ons are installed and working fine. For example, they load fine from the manual PDF links on this page: http://dar-us-salam.com/eCatalog.htm Link to comment Share on other sites More sharing options...
Darussalam Posted March 2, 2016 Author Share Posted March 2, 2016 Is it possible to get the attachment link directly and bypass the AttachmentController.php all together? That will make it easier to open and force a new Tab/Window. I tried this but it gives Internal Server Error 500 at the link where the file should be: <a class="btn btn-default btn-block" href="http://dar-us-salam.com/download/{$attachment.file_name|escape:'html':'UTF-8'}"> Second question: Is it possible to re-calculate the file sizes for the attachments as when I did bulk import with Store Commander it made them all 0 (only the initial 50 or so were calculated properly). When I go to each one and update, they calculate fine. {if isset($attachments) && $attachments} <!--Download --> <section class="page-product-box"> <h3 class="page-product-heading">{l s='Sample Pages'}</h3> {foreach from=$attachments item=attachment name=attachements} {if $smarty.foreach.attachements.iteration %3 == 1}<div class="row">{/if} <div class="col-lg-4"> <h4><a href="{$link->getPageLink('attachment', true, NULL, "id_attachment={$attachment.id_attachment}")|escape:'html':'UTF-8'}">{$attachment.name|escape:'html':'UTF-8'}</a></h4> <p class="text-muted">{$attachment.description|escape:'html':'UTF-8'}</p> <a class="btn btn-default btn-block" href="{$link->getPageLink('attachment', true, NULL, "id_attachment={$attachment.id_attachment}")|escape:'html':'UTF-8'}"> <i class="icon-download"></i> {l s="View Pages"} ({Tools::formatBytes($attachment.file_size, 2)}) </a> <hr /> </div> {if $smarty.foreach.attachements.iteration %3 == 0 || $smarty.foreach.attachements.last}</div>{/if} {/foreach} </section> <!--end Download --> {/if} Link to comment Share on other sites More sharing options...
bellini13 Posted March 3, 2016 Share Posted March 3, 2016 https://www.prestashop.com/forums/topic/252894-solved-open-pdf-in-new-window/?p=2269307 Link to comment Share on other sites More sharing options...
Darussalam Posted March 4, 2016 Author Share Posted March 4, 2016 Hi, After banging my head for so long on this problem I finally figured it out after looking at the headers where were being loaded for that file (Inspect > Network > look at the file to be downloaded). It was loading Content-Type: application/octet-stream so I manually changed this line: header('Content-Type: '.$a->mime); to following: header('Content-Type: application/pdf'); and waolla everything working normally! Now it opens the PDF without the download window poping up. Now, however, I need to know the correct code or some fix so that will make it work properly even if I have an mp3 as attachment. Any help will be appreciated. Thanks Omar Link to comment Share on other sites More sharing options...
bellini13 Posted March 4, 2016 Share Posted March 4, 2016 This is the correct code to use... $a->mime You need to figure out why 'application/pdf' was not being returned, assuming it is a PDF Link to comment Share on other sites More sharing options...
Darussalam Posted March 6, 2016 Author Share Posted March 6, 2016 Hi, I just realized this problem is probably caused by the mass uploading attachments though Store Commander. I have over a thousand PDF files which I just can't upload manually and was lucky to find mass attachment upload and CSV assignment to products feature in Store Commander. The file I manually uploaded is named as ddfc17a04faa99c86c2ddcf0ee71531467c4091c while SC attachments are named just as I uploaded them but with file size of 0. Link to comment Share on other sites More sharing options...
Darussalam Posted March 23, 2016 Author Share Posted March 23, 2016 Just for someone having the same problem in future: I finally figured out the root cause when looking at the 'attachment' table in myPHPAdmin. The mime column had some PDF set as application/octet-stream so I ran this command and changed all of them to application/pdf with the following command: UPDATE `attachment` SET `mime` = "application/pdf" Someone can add a where statement if there are other formats as well. Link to comment Share on other sites More sharing options...
rempaddesign Posted November 26, 2020 Share Posted November 26, 2020 If this problem is stil in the to do's list for anyone the solution for the latest Prestashop version (1.7.6.8) for opening PDF's or other documents in browser windows is to change 2 things:1. Change the controllers/front/AttachmentController.php file like so: ORIGINAL: header('Content-Disposition: attachment; filename="' . utf8_decode($a->file_name) . '"'); CHANGED: header('Content-Disposition: inline; filename="' . utf8_decode($a->file_name) . '"');2. Change the .htaccess content of this line: <FilesMatch "\.pdf$"> Header set Content-Disposition "Attachment" /// Alter this line like so: Header set Content-Disposition "Inline" Header set X-Content-Type-Options "nosniff" </FilesMatch> Hope this will help someone. All the best! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now