Jump to content
  • 0
Darussalam

Pdf Files Download Instead Of Opening

Question

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

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0

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;
    }
}

Share this post


Link to post
Share on other sites
  • 0

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}

Share this post


Link to post
Share on other sites
  • 0
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

Share this post


Link to post
Share on other sites
  • 0

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.

Share this post


Link to post
Share on other sites
  • 0

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More