Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Sir Osis of Liver on July 15, 2022, 12:51:14 PM

Title: pdf download
Post by: Sir Osis of Liver on July 15, 2022, 12:51:14 PM
Is there any way to make a pdf file download instead of open in browser?
Title: Re: pdf download
Post by: Arantor on July 15, 2022, 01:03:29 PM
The attachment code tells the browser to take it as a download; the browser chooses to ignore this.

The only way you might get somewhere is if the code is changed to lie about the content but 1) browsers might still ignore it and 2) malware checkers might not like it much either.
Title: Re: pdf download
Post by: Sir Osis of Liver on July 15, 2022, 01:12:56 PM
They're not attachments, there are two buttons, first one links to pdf and allows users to read online, browser opens it.  Second one is for direct download, but pdfs are zipped, which prevents browser from opening it, but requires user to unzip it, which may be problematic for the unwashed, especially on mobile.

Title: Re: pdf download
Post by: Arantor on July 15, 2022, 01:16:40 PM
OK, so let's pretend for a minute that I'm *not* a mindreader.

Are they real buttons attached to a form or just things that look like buttons but using a regular link?

Are you just linking to the files on the server in a folder somewhere?
Title: Re: pdf download
Post by: Sir Osis of Liver on July 15, 2022, 01:20:59 PM
https://ebook-mecca.com/index.php?topic=405.msg405#msg405

As you can see, READ ONLINE links to pdf, PDF links to zip of same file.

Title: Re: pdf download
Post by: Arantor on July 15, 2022, 01:32:52 PM
In each of the folders that has a .pdf file you want to download, try adding an .htaccess file with the following:

<FilesMatch "\.pdf$">
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</FilesMatch>

Note that this won't work if you want to somehow put them in the same folder as the read-online version.
Title: Re: pdf download
Post by: Sir Osis of Liver on July 15, 2022, 01:41:20 PM
Will try that.  The files are currently in different subdirectories.  The read online pdfs are all in one, the zips are in 60 subdirs (by category).  I have a script I can use to change file extensions from .zip to .pdf, but haven't figured out how to apply it recursively to subdirs, so I'd have to run it 60 times (and upload the .htaccess 60 times).
Title: Re: pdf download
Post by: Kindred on July 15, 2022, 01:43:12 PM
Changing the extension does not change the file type!!   If those are actually zipped pdfs, then changing the extension to .pdf from .zip would cause all sorts of troubles
Title: Re: pdf download
Post by: Arantor on July 15, 2022, 01:45:17 PM
Yup, because what is happening here is Apache will *think* the file is a PDF (because it won't actually check against the file content), serve it up with the application/pdf MIME type and virus scanners will absolutely freak out when they see a file that claims to be application/pdf but is in fact not.

As a clue: the first 3 bytes of a PDF file literally spell 'PDF'; the first 2 bytes of a zip file spell 'PK' after the guy who invented the format. Renaming it doesn't rewrite its content.
Title: Re: pdf download
Post by: Sir Osis of Liver on July 15, 2022, 01:53:11 PM
(https://thekrashsite.com/pics/fp.gif) Didn't think of that.  The .htaccess works, but would require replacing all the zips with the original pdfs.  We're setting up a separate support forum linked from the booksite (which is no longer a forum), will have to include guidance for handling various file types.  Anyway, thanks.
Title: Re: pdf download
Post by: Sir Osis of Liver on July 17, 2022, 12:07:00 AM
Any idea why the .htaccess worked yesterday, tonight it doesn't?  Browser loads pdf with or without.
Title: Re: pdf download
Post by: Sir Osis of Liver on July 17, 2022, 06:16:57 PM

<FilesMatch "\.pdf$">
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</FilesMatch>


Doesn't work.

HTML5 'download' attribute doesn't work (not cross-origin problem, file in same directory).

Stumped. >:(