This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Writing to output stream

tsikes wrote on 2010-06-11:
Do you have any sample code usage in c# or vb?

This works for hard coding a file to convert, but i want to send it in a stream to the user for download. How do I get the PDF bytes to binary write?
support wrote on 2010-06-11:
A quick tip:

The conversion methods accept System.IO.Stream. In our examples we use System.IO.FileStream but you can pass an instance of any class that is inherited from System.IO.Stream instead.

See http://msdn.microsoft.com/en-us/library/system.io.stream.aspx for documentation on System.IO.Stream interface and the list of available derived stream classes.

Does this answer your question?
tsikes wrote on 2010-06-11:
Thanks.. no i did not realize that.. I'm using memory stream now.. below is my code, it sends the stream to the browser and creates the pdf however when I attempt to open the newly created pdf it states that the file is damaged or wasn't properly decoded..

I couldn't get the microsoft page you sent to pull up. What am I doing wrong? Below is my code in both vb.net and c#.net

-------------------------------------------------------------------------------------------------------------------------
VB.NET
-------------------------------------------------------------------------------------------------------------------------

Dim Stream As New MemoryStream
Dim client As New pdfcrowd.Client("user", "code")
client.convertURI(URL, Stream)
'client.setOwnerPassword("test")
Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
Dim Buffer(Stream.Length) As Byte
Stream.Read(Buffer, 0, CInt(Stream.Length))
Stream.Close()
Response.Clear()
Response.AddHeader("Content-Type", "binary/octet-stream")
Response.AddHeader("Content-Disposition", "attachment; filename=ConversionReport.pdf; size=" & Buffer.Length.ToString())
Response.Flush()
Response.BinaryWrite(Buffer)
Response.Flush()
Response.End()

-------------------------------------------------------------------------------------------
C#.net
-------------------------------------------------------------------------------------------

MemoryStream Stream = new MemoryStream();
pdfcrowd.Client client = new pdfcrowd.Client("user", "code");
client.convertURI(URL, Stream);
//client.setOwnerPassword("test")
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
byte[] Buffer = new byte[Stream.Length + 1];
Stream.Read(Buffer, 0, Convert.ToInt32(Stream.Length));
Stream.Close();
Response.Clear();
Response.AddHeader("Content-Type", "binary/octet-stream");
Response.AddHeader("Content-Disposition", "attachment; filename=ConversionReport.pdf; size=" + Buffer.Length.ToString());
Response.Flush();
Response.BinaryWrite(Buffer);
Response.Flush();
Response.End();
support wrote on 2010-06-12:
Try this (untested):

MemoryStream Stream = new MemoryStream();
pdfcrowd.Client client = new pdfcrowd.Client("user", "code");
//client.setOwnerPassword("test")
client.convertURI(URL, Stream);
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=ConversionReport.pdf");
Response.AddHeader("Content-Size", Stream.Length.ToString());
Stream.CopyTo(Response.OutputStream);
Stream.Close();
Response.Flush();
Response.End();
tsikes wrote on 2010-06-13:
This doesn't work either. I've tried just about every code setup possible and I'm still getting this damaged file error when attempting to open the saved file. I noticed your site is PHP.. maybe this just doesn't work in .net?
support wrote on 2010-06-13:
The API is platform independent and it works in .NET.

Please, could you attach that damaged file?
tsikes wrote on 2010-06-15:
Do you have an email address I can send it to? I don't see a way to attach a file to this forum.
support wrote on 2010-06-15:
There should be a link right above the reply text box that lets you attach a file.

If you can't see it please send the file to info@pdfcrowd.com.
tsikes wrote on 2010-06-15:
I sent it as a file attachment.. FYI.. the report link expires everyday. Here is a new URL for today if you want to test this yourselves.. Site is still in development.

http://www.teecalc.com/calculator/report.aspx?ID=kz2HIHRRW6Y%3d&DX=w1R3b7cMFGW4wr8OgLExYQ%3d%3d

Could this have something to do with the format of the report?
support wrote on 2010-06-15:
We have received the pdf but its size is zero. Our logs show that our service successfully generated a pdf from that URL and that it was received on your end.

Are you at least able to save it to a file as shown in our C# example (http://pdfcrowd.com/api/#csharp)?

Sadly, we are not familiar with web development under Windows, but anyway here is another untested try:
--
MemoryStream Stream = new MemoryStream();
pdfcrowd.Client client = new pdfcrowd.Client("user", "code");
client.convertURI(URL, Stream);
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=\"ConversionReport.pdf\"");
Stream.CopyTo(Response.OutputStream);
Stream.Close();
Response.Flush();
Response.End();
--

Also, could you somehow verify that Stream.Length is non-zero when client.convertURI() finishes?
tsikes wrote on 2010-06-15:
I wasn't able to use your code because CopyTo is not a member of Stream in vb.net. My project is actually vb.net so i've converted your code to this: which is giving me the error. I'll trouble shoot some more and let you know what i find.

Dim Stream As New MemoryStream()
Dim client As New pdfcrowd.Client("user", "code")
client.convertURI(URL, Stream)
Dim Response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=""ConversionReport.pdf""")
Stream.CopyTo(Response.OutputStream)
Stream.Close()
Response.Flush()
Response.[End]()
tsikes wrote on 2010-06-15:
O and.. Stream.length is 66502 so it is filling the stream. Just having issues passing it to the browser. I'll work on it some more.
cyberworldukltd wrote on 2012-01-24:
This works for hard coding a file to convert, but i want to send it in a stream to the user for download.

Regards
Cyberworldukltd
Latest Cheap Mobile Phone Accessories Shop uk
support wrote on 2012-01-24:
Hello,

to send the stream with the generated PDF to the user you need to copy it to System.Web.HttpContext.Current.Response.

You should also set proper HTTP headers:
Response.ContentType = "application/pdf" 
Response.AddHeader("Content-Disposition", "attachment; filename=ConversionReport.pdf")

The headers above specify that the response contains a PDF file named "ConversionReport.pdf" and that the browser should open the "Save File As" dialog.
bradnywells wrote on 2015-10-09:
Check this one......MemoryStream to FileStream

Wells