Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a pdf to a memory stream

I am writing an application in MVC4.

I have a physical pdf file on the server. I want to convert this to a memory stream and send it back to the user like this:

return File(stream, "application/pdf", "myPDF.pdf");

But how do I convert a pdf file to a memory stream?

Thanks!

like image 274
user2206329 Avatar asked Feb 17 '14 10:02

user2206329


People also ask

How do I convert files to stream?

First create FileStream to open a file for reading. Then call FileStream. Read in a loop until the whole file is read. Finally close the stream.

How do I turn a PDF into an embedded link?

Choose Tools, click on Edit PDF, then on Link, and finally, select Add/Edit Web or Document Link. Select the area that you want to hyperlink. Then, in the Create Link dialog box, choose the options you want for the link appearance and click on the Open a Web Page button for the link action. Hit Next and enter the link.

Can I turn a PDF into a URL?

Open the file you want to convert in your PDF editor. Select the Create & Edit button on the right-side toolbar. Click Export PDF at the top of the window. Choose HTML Web Page and select your options.


1 Answers

You don't need MemoryStream. Easiest way is to use overload that accepts file name:

return File(@"C:\MyFile.pdf", "application/pdf");

another solution is to use overload that accepts byte[]:

return File(System.IO.File.ReadAllBytes(@"C:\Myfile.pdf"), "application/pdf");

or if you want use FileStream:

return File(new FileStream(@"C:\MyFile.pdf", FileMode.Open, FileAccess.Read), "application/pdf");
like image 137
dkozl Avatar answered Sep 21 '22 11:09

dkozl