Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download pdf programmatically

How can I download a PDF and store to disk using vb.NET or C#?

The URL (of the PDF) has some rediection going on before the final PDF is reached.

I tried the below but the PDF seems corrupted when I attempt to open locally,

Dim PdfFile As FileStream = File.OpenWrite(saveTo)
Dim PdfStream As MemoryStream = GetFileStream(pdfURL)
PdfStream.WriteTo(PdfFile)
PdfStream.Flush()
PdfStream.Close()
PdfFile.Flush()
PdfFile.Close()
like image 317
Perplexed Avatar asked May 26 '10 14:05

Perplexed


People also ask

How do I download a PDF in HTML?

With the use of the <a> tag download attribute, we can download pdf files, images, word files, etc. The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink.

How can I download a PDF from a URL using Javascript?

dispatchEvent(new MouseEvent('click')); } var fileURL = "link/to/pdf"; var fileName = "test. pdf"; download(fileURL,fileName); The code above is just to test download one file from a hardcoded URL. If it worked as intended, when the page is loaded, it should download the pdf from the provided url.

How do I download a PDF URL?

Just open the link of the PDF in a browser, then click the three-dot icon and select Download.

How do I download a PDF from react JS server?

It is possible by using the fetch() method provided by Java Script. The PDF file will be placed in the public folder of React JS application folder structure.


2 Answers

You can try to use the WebClient (System.Net namespace) class to do this which will avoid any stream work on your side.

The following C# code grabs an IRS form and saves it to C:\Temp.pdf.

using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", @"C:\Temp.pdf");
}
like image 112
Pat Avatar answered Oct 19 '22 21:10

Pat


You can also try the following code sample to download pdf files

 Response.ContentType = "Application/pdf"; 
 Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); 
 Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf")); 
 Response.End(); 
like image 45
Raaghav Avatar answered Oct 19 '22 21:10

Raaghav