-1

[Note this might be similar to https://stackoverflow.com/questions/48138874/can-make-print-js-print-a-variable, but I don't know PHP]

I have an ASP.Net core action that creates a PDF on the fly. I currently have the PDF download to the client, like so:

<a asp-controller="Home" asp-action="Pdf">Download PDF</a>

and the controller action

public IActionResult Pdf()
{
    using (MemoryStream ms = new MemoryStream())
    {
        ...
        return File(ms.ToArray(), "application/pdf", "file.pdf");
    }
}

Instead, I would like it to go to the print preview dialog of the browser, for which I was planning to use printjs. But I have to specify a server-based file (such as "docs/file.pdf"). The printjs sample is:

<button type="button" onclick="printJS('docs/file.pdf')">Print PDF</button>

Is there a way to cause the printJS file to download the pdf file without needing to save it somewhere?

Naeem Khoshnevis
  • 2,001
  • 4
  • 18
  • 30
erict
  • 1,394
  • 2
  • 14
  • 28
  • Interesting, I know that CanadaPost does this somehow. I suppose an ajax call can generate the file onto the server's disk somewhere with a unique name and then return that name to the browser which then uses print.js by asking for that file – erict Jan 12 '22 at 15:49

1 Answers1

0

Doh. Too easy:

<a onclick="printJS('/home/Pdf')">Print PDF</a>

Instead of supplying an href to a file, have the onclick function call printJS with the action name which will execute and download the PDF.

erict
  • 1,394
  • 2
  • 14
  • 28