“name” web pdf for better default save filename in Acrobat?

后端 未结 16 1721
难免孤独
难免孤独 2020-12-14 00:09

My app generates PDFs for user consumption. The \"Content-Disposition\" http header is set as mentioned here. This is set to \"inline; filename=foo.pdf\", which should be

相关标签:
16条回答
  • 2020-12-14 01:06

    Set the file name in ContentType as well. This should solve the problem.

    context.Response.ContentType = "application/pdf; name=" + fileName;
    // the usual stuff
    context.Response.AddHeader("content-disposition", "inline; filename=" + fileName);
    

    After you set content-disposition header, also add content-length header, then use binarywrite to stream the PDF.

    context.Response.AddHeader("Content-Length", fileBytes.Length.ToString());
    context.Response.BinaryWrite(fileBytes);
    
    0 讨论(0)
  • 2020-12-14 01:06

    The way I solved this (with PHP) is as follows:

    Suppose your URL is SomeScript.php?id=ID&data=DATA and the file you want to use is TEST.pdf.

    Change the URL to SomeScript.php/id/ID/data/DATA/EXT/TEST.pdf.

    It's important that the last parameter is the file name you want Adobe to use (the 'EXT' can be about anything). Make sure there are no special chars in the above string, BTW.

    Now, at the top of SomeScript.php, add:

    $_REQUEST = MakeFriendlyURI( $_SERVER['PHP\_SELF'], $_SERVER['SCRIPT_FILENAME']);
    

    Then add this function to SomeScript.php (or your function library):

    function MakeFriendlyURI($URI, $ScriptName) {
    
    /* Need to remove everything up to the script name */
    $MyName = '/^.*'.preg_quote(basename($ScriptName)."/", '/').'/';
    $Str = preg_replace($MyName,'',$URI);
    $RequestArray = array();
    
    /* Breaks down like this
          0      1     2     3     4     5
        PARAM1/VAL1/PARAM2/VAL2/PARAM3/VAL3
    */
    
    $tmp = explode('/',$Str);   
    /* Ok so build an associative array with Key->value
       This way it can be returned back to $_REQUEST or $_GET
     */
    for ($i=0;$i < count($tmp); $i = $i+2){
        $RequestArray[$tmp[$i]] = $tmp[$i+1];
    }
    return $RequestArray;       
    }//EO MakeFriendlyURI
    

    Now $_REQUEST (or $_GET if you prefer) is accessed like normal $_REQUEST['id'], $_REQUEST['data'], etc.

    And Adobe will use your desired file name as the default save as or email info when you send it inline.

    0 讨论(0)
  • 2020-12-14 01:11

    Apache's mod_rewrite can solve this.

    I have a web service with an endpoint at /foo/getDoc.service. Of course Acrobat will save files as getDoc.pdf. I added the following lines in apache.conf:

    LoadModule     RewriteModule         modules/mod_rewrite.so
    RewriteEngine  on
    RewriteRule    ^/foo/getDoc/(.*)$    /foo/getDoc.service     [P,NE]
    

    Now when I request /foo/getDoc/filename.pdf?bar&qux, it gets internally rewritten to /foo/getDoc.service?bar&qux, so I'm hitting the correct endpoint of the web service, but Acrobat thinks it will save my file as filename.pdf.

    0 讨论(0)
  • 2020-12-14 01:12

    Credits to Vivek.


    Nginx

    location /file.pdf
    {
        # more_set_headers "Content-Type: application/pdf; name=save_as_file.pdf";
        add_header Content-Disposition "inline; filename=save_as_file.pdf";
        alias /var/www/file.pdf;
    }
    

    Check with

    curl -I https://example.com/file.pdf
    

    Firefox 62.0b5 (64-bit): OK.

    Chrome 67.0.3396.99 (64-Bit): OK.

    IE 11: No comment.

    0 讨论(0)
提交回复
热议问题