Print Page | Close Window

Creating PDF Streaming (no saving file to disk)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2009
Printed Date: 02 May 25 at 12:54PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Creating PDF Streaming (no saving file to disk)
Posted By: Dan
Subject: Creating PDF Streaming (no saving file to disk)
Date Posted: 21 Oct 11 at 2:43PM
Hi,
Does anyone have a VB or C# example of how I can send a pdf directly to the response stream?

All the examples I see save the pdf to disk using code such as:

Dim pdf As New QuickPDF.Client
pdf.Connect(ConnectURL, ConnectPort)
pdf.UnlockKey(QuickPDFKey)
pdf.SetOrigin(origin)

pdf.DrawText(100, 100, "Testing 123")
pdf.SaveToFile("../pdf/HelloWorld.pdf")

I'd like to just return the dynamically created pdf when a user opens a certain URL. Is this possible?

Thanks,
Dan

Edit: What I want to do is very similar to what they do with itextsharp here: http://alandennis.blogspot.com/2005/07/streaming-pdf-using-itextsharp-and.html - http://alandennis.blogspot.com/2005/07/streaming-pdf-using-itextsharp-and.html , just with quick pdf.



Replies:
Posted By: samb
Date Posted: 21 Oct 11 at 6:18PM
Assuming you're using the .net dll wrapper  C# would be:
byte[] pdfContents = pdf.SaveToStream() pdf.SaveToString()

http://www.quickpdflibrary.com/help/quickpdf/SaveToString.php

In the .net wrapper, SaveToString requires no arguments and actually returns a byte array, not a string.


Posted By: Dan
Date Posted: 21 Oct 11 at 9:58PM
According to the docs, http://www.quickpdflibrary.com/help/quickpdf/SaveToStream.php - http://www.quickpdflibrary.com/help/quickpdf/SaveToStream.php SaveToStream() is only available in the Delphi version of quick pdf.

I will try the SaveToString() method.

Thanks,
Dan


Posted By: Dan
Date Posted: 24 Oct 11 at 4:23PM
Works

Here is the rest of the code (VB.Net):

Dim filename As String = "Testing.pdf"
Dim bytes As Byte() = pdf.SaveToString()
Dim m As System.IO.MemoryStream = New System.IO.MemoryStream(bytes, 0, bytes.Length, False, True)

Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)

Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer.Length)

Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.End()


Posted By: Dan
Date Posted: 24 Oct 11 at 4:56PM
Not sure if it's best to leave out the MemoryStream and just use the byte array, but this also works:

Dim pdf As New QuickPDF.Client
pdf.Connect(ConnectURL, ConnectPort)
pdf.UnlockKey(QuickPDFKey)
pdf.SetOrigin(origin)
pdf.DrawText(100, 100, "Testing 123")

Dim filename As String = "Testing.pdf"
Dim bytes As Byte() = pdf.SaveToString()

Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)

Response.OutputStream.Write(bytes, 0, bytes.Length)

Response.OutputStream.Flush()
Response.OutputStream.Close()
Response.End()


Posted By: samb
Date Posted: 24 Oct 11 at 7:00PM
Sorry, you are correct: SaveToString is what I meant to type. Embarrassed

As for the Memory Stream, you're usually supposed to dispose them when you're done (either wrap it in a using block, or explicitly call dispose).  Otherwise, resources may not be freed up when you think they do. 

Since your getting a byte array and streaming a byte array, there's no reason to convert it to a memory stream in the middle.  Also, since the byte array doesn't need any cleanup, you don't have to worry about resources.


Posted By: Dan
Date Posted: 24 Oct 11 at 8:39PM
Sounds good. Thanks for the help!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk