Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pdf to stream from url

I have a PDF which is hosted in say http://test.com/mypdf.pdf.

How can I convert the PDF to Stream and then using this Stream convert it back to PDF.

I tried the following but got an exception(see image):

private static Stream ConvertToStream(string fileUrl)
{
    HttpWebResponse aResponse = null;
    try
    {
        HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(fileUrl);
        aResponse = (HttpWebResponse)aRequest.GetResponse();
    }
    catch (Exception ex)
    {

    }

    return aResponse.GetResponseStream();
}

enter image description here

like image 906
Zaki Avatar asked Nov 01 '12 11:11

Zaki


2 Answers

This will work:

private static Stream ConvertToStream(string fileUrl)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    try {
        MemoryStream mem = new MemoryStream();
        Stream stream = response.GetResponseStream();

        stream.CopyTo(mem,4096);

        return mem;
    } finally {
        response.Close();
    }
}

However you are entirely responsible for the lifetime of the returned memory stream.

A better approach is:

private static void ConvertToStream(string fileUrl, Stream stream)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    try {
        Stream response_stream = response.GetResponseStream();

        response_stream.CopyTo(stream,4096);
    } finally {
        response.Close();
    }
}

You can then do something like:

using (MemoryStream mem = new MemoryStream()) {
    ConvertToStream('http://www.example.com/',mem);
    mem.Seek(0,SeekOrigin.Begin);

    ... Do something else ...
}

You may also be able to return the response stream directly but you'd have to check on the lifetime of that, releasing the response may release the stream, hence the mem copy.

like image 181
Lloyd Avatar answered Nov 20 '22 15:11

Lloyd


You may want to take a look at WebClient.DownloadFile.

You give it a URL and local file name and it saves the file straight to disk. Might save you a step or two.

You could also try WebClient.DownloadData which saves the file to an in-memory byte[].

EDIT

You did not specify the protocol of the web-service you are posting the file to. The simplest form (RESTful) would be just to POST the file to data to another URL. Here is how you would do that.

using (WebClient wc = new WebClient())
{
    // copy data to byte[]
    byte[] data = wc.DownloadData("http://somesite.com/your.pdf");

    // POST data to another URL
    wc.Headers.Add("Content-Type","application/pdf");
    wc.UploadData("http://anothersite.com/your.pdf", data);
}

If you are using SOAP, you would have to convert the file to a Base64 string, but hopefully you are using a generated client which takes care of that for you. If you could elaborate on the type of web-service you are sending the file to, I could probably provide some more information..

like image 5
dana Avatar answered Nov 20 '22 16:11

dana