How do I force the browser to download a pdf file instead of opening it in a tab?

  • 0
    Hello!

    Such a situation:
    There is a simple landing page with a form, after filling in which a PDF file should be downloaded.
    But instead, it opens in a browser window and it turns out that the user needs to take extra steps to download it.

    Question:
    Is it possible to somehow influence the content settings of the client's browser so that this pdf file does not open in the tab, but is immediately offered to download / save?
    JavaScript Aidan Barker, Dec 14, 2020

  • 4 Answers
  • 0
    serve with the header Content-Type: application / octet-stream
    Anonymous

  • 0
    Now every first browser considers itself the best pdf viewer, so the server settings for forced download are easily ignored depending on the browser settings. Typically, the default settings are in favor of the browser.

    It's easier not to fight this, but to ask the question: why should the user download the file? If anything, you can save it from the viewer.
    Anonymous

  • 0
    Content-Disposition: attachment; filename="filename.pdf"


    The header must be given by the server.
    Ronan Hunt

  • 0
    Alternatively, attach a link with the download attribute to the form submit button.

    <form>
    <input type="text">
    <input type="text">
    <a href="ссылка на файл" download><button type="submit">Отправить форму</button></a>
    </form>
    Logan Bryant

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!