Forum How do I...?

Dynamic images in PDF

brendan
Hi

I have a website that has dynamic images. by that i mean that the web page has place holders and the images are streamed into the placeholders from the database.
How do I stream these images into to the PDF.
Code:
Response.ContentType = "image/jpeg"
Response.BinaryWrite(dataSet.Tables(0).Rows(0).Item(0))

PDF Code:
Response.Clear()
Response.Filter = New PrinceFilter(princeConverter, Response.Filter)
Response.AddHeader("Content-Type", "application/pdf")
Response.Flush()

The problem is that I cannot change the header as I have already written the images to the page on a post back.

Brendan
mikeday
So you are using ASP.NET, and wish to generate a PDF to send back to the browser. The PDF is created by using Prince as a filter to take a normal HTML page response from your site and convert it to PDF. This HTML page includes image references, eg. <img src="..."> which link to other URLs on your site; correct so far?

What exactly do you mean by image place holders? Does the img element point to one image, and then you change it and make it point to another? Or it just points to a URL that supplies an image loaded on the fly from the database?

Prince can load images from URLs similarly to the browser, so that may not be an issue. What are you currently getting in the PDF output?
brendan
Sorry for the delay in coming back to you.
I have sorted this out. The placeholder is a url that point to a page that gets the image from the database.
I had to create a second website to deal with the image. the second website would binary write the image to the required position in the PDF.
It was tricky getting the permissions right between all the sites, but all is working now.

Thanks again
mickyjtwin
I am interested in this post, in that our images are all high-res tiffs stored on the filesystem. The user can go in a change the height/width of the image, but I am also looking at implementing a "cropping" feature. With this, the image would have to be streamed to prince via a .net handler, e.g.
<img src="imagehandler.ashx?id=123&height=50&width=50&cropx=4&cropy=8&cropwidth=50&cropheight=20"/>
Has anyone done this before?
mikeday
This should work fine on the Prince side of things, it's just a matter of setting up the handler on the server and then testing if it works when viewing the page in a browser. And perhaps escaping the ampersand characters as &amp in the URL to ensure that they don't accidentally get replaced with named character entities? :)
jesse
mickyjtwin, Were you successful in calling your imagehandler page with params? I am trying to implement a very similar scenario. I get a proper image when I ping the URL from my browser, however, once I send the html off to Prince, I am not seeing my "imagehandler" called and consequently no image is getting returned into my PDF. My url is something like the following:

<img src="http://127.0.0.1:7101/myapp/faces/adf.task-flow?adf.tfDoc=%2FWEB-INF%2Fimage.xml&amp;imageId=image1&amp;imageName=warningSign">


I've discovered in the Prince log that I have the following warning:

warning: Unknown image MIME type: text/html


But I am setting my response's contentType to "image/jpeg".

Any clues?
mikeday
Can you try visiting that URL with a browser or a program like curl or wget and see what you get back?
jesse
Using curl to visit this URL, I get the following:

ÿOÿà ►JFIF ☺☻  ☺ ☺  ÿU C♠♠♠♣
↑2!∟!22222222222222222222222222222222222222222222222222ÿA ◄? ?♥☺" ☻◄☺♥◄☺ÿÄ♀▼  ☺♣☺☺☺☺☺☺        ☺☻♥♦♣♠
♂ÿÄ µ► ☻☺♥♥☻♦♥♣♣♦♦  ☺}☺☻♥ ♦◄♣↕!1A♠‼Qa"q¶2?`#B±A§RÑd$3br,
...

That goes on for a while. Is this what I would expect to see when viewing an image using cURL?

And I did mention this above, but I was able to get back a proper image when visiting this URL with my browser, for which has the following header information:

Response Headers
Date Tue, 28 Jul 2009 16:13:33 GMT
Transfer-Encoding chunked
Content-Type image/jpeg
X-Powered-By Servlet/2.5 JSP/2.1, JSF/1.2

Request Headers
Host 127.0.0.1:7101
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR
3.5.30729)
Accept image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Proxy-Connection keep-alive

p.s. It also made a lot of system beeps! :)
jesse
I've gotten my issue resolved, and it was very specific to the implementation of my application. I know what I have changed (bypassed the JSF part, and stuck with just plain JSP), but I am not sure quite exactly why it works now. Thanks for your reply.