Search code examples
javaplayframeworkplayframework-2.0pdfbox

how to specify the pdfbox.apache filename when downloading/exporting?


Just wondering is there any way to name the document after you specify the doc.name to a template

  PDDocument doc = PDDocument.load(play.Play.application().resource("/templates/" + FileName));

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            doc.save(byteArrayOutputStream);
            doc.close();

therefore, when you download PDFBOX rendered file, the name of the pdf file can not be specified. is there any other way to do it?


Solution

  • I am not familiar with Play framework.

    If you want to enable users to download the file and provide it a filename, then you need to set the HTTP header

    Content-Disposition: attachment; filename=myfile.pdf
    

    When the browser sees this header, the user will get a dialog box to save the file and will suggest the name to be myfile.pdf.