0
votes

I need to automatically print the pdf file generated. First, I save it to the server after generating the pdf file and then download it to client machine. I need to print the pdf file downloaded from the client machine. Is there a way I could get the exact location file where the pdf file is saved after downloaded? Is there a way I can print directly the pdf file using the client/local printer? For now, I have used spire pdf and the problem is it uses the server's printer and i need to fix it to use the client/local printer.

Codes:

window.location.href = "/Orders/Download?file=" + encodeURIComponent(data.fileName);

Controller:

[HttpGet]
[DeleteFileAttribute] //Action Filter, it will auto delete the file after download
public ActionResult Download(string file)
{
   //get the temp folder and file path in server
   string fullPath = Path.Combine(Server.MapPath("~/Content/PDF"), file);

   try
   {
       PdfDocument doc = new PdfDocument();
       doc.LoadFromFile(fullPath);
       doc.PrinterName = "HP Deskjet Ink Adv 2010 K010";
       //Use the default printer to print all the pages
       doc.PrintDocument.Print();
   }
   catch (Exception e)
   {
       var message = e.Message;
   }

   return File(fullPath, "application/pdf", file);
}

Is this code, it will download the pdf file to client/user Downloads or where the user set the location to download and it will print as well however it uses the server's printer which is the problem.

1
Short answer - No. You have no control over the clients browser or their printers.user3559349
Is there a way i could get the the downloads location of the user where the pdf is downloaded ?Jen143
No. You have no access to the clients file systemuser3559349
Is there other way I could print automatically the pdf file?Jen143
No (which is fortunate because the web would not exist if you could). But I don't understand why you would want to do that. How would you like it if you navigated to a site and your printer just started spitting out documents.user3559349

1 Answers

0
votes

I think this is not possible in modern browsers.

Most solutions I saw (here, and there) rely on an embedded PDF that is loaded and printed. However, iframes are nowadays sandboxed and since PDFs usually rely on a native plugin you are out of luck: plugins will not load - and you cannot change that.

Here is sample code that yields the error:

Failed to load '..sample.pdf' as a plugin, because the frame into which the plugin is loading is sandboxed.

$('#pdfDocument').attr("src", "https://upload.wikimedia.org/wikipedia/commons/8/85/I-20-sample.pdf").load(function(){
    document.getElementById('pdfDocument').contentWindow.print();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<embed sandbox="allow-top-navigation allow-scripts allow-popups allow-forms"
type="application/pdf"
src="https://upload.wikimedia.org/wikipedia/commons/8/85/I-20-sample.pdf"
id="pdfDocument"
width="100%"
height="100%" />