Avatar of QuestionManA
QuestionManA
 asked on

ASPpdf - doc.savehttp

I'm certain that this is a very stupid question but for the life of me I can't seem to figure it out.
I have a function on a classic asp page that creates a pdf the asppdf component. However when I use Doc.SaveHttp "attachment;filename=\aspPDFtemp\"& pdffile&",application/pdf"
I'm not prompted to save or open the pdf, but rather the below is displayed

Any ideas?

%PDF-1.3 %ýâ 1 0 obj <> endobj 2 0 obj <> endobj 3 0 obj <> endobj 4 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> stream xýýýy|ýýý?|ýsóeý{&ý%ýýýýL2Ifý3!ý;daIý4EKW$Vý Tý[[@-Jý$,ý5ýj*]ýjý`ý¶FýEýý$ýý™ýýýý}ý?ýýýLýý:ý9ý9ývýL ýý~@ZývýýEo\ý9ýP=wQò

When I use Doc.Save( Server.MapPath("\aspPDFtemp\"& pdffile), False )  the pdf is created and I can link to it.
ASP

Avatar of undefined
Last Comment
QuestionManA

8/22/2022 - Mon
ajnt__

Can you try setting the response content type?
Response.ContentType = "application/pdf"
ASKER CERTIFIED SOLUTION
ajnt__

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
QuestionManA

ASKER
Thanks for the quick reply.

The issue is when I use doc.savehttp which does not save the pdf to the server but instead streams it.
So when I use: Doc.SaveHttp "attachment;filename=\aspPDFtemp\"& pdffile&",application/pdf" the encoded text is displayed.
SOLUTION
CCongdon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CCongdon

And that's what the ajnt__'s code does. It tells the browser not to expect text, but to expect a PDF file. The problem is both a server and a client problem. The client isn't being told what to expect so it defaults to MIME type of text/html. So that's what these lines do. They tell IIS to inform the client that Text is NOT what's coming next.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ajnt__

Thanks for the help CCongdon. Glad you agree :-)
QuestionManA

ASKER
I created a test asp page that only contains the attached code.
The output is the same.
Couldn't I just add the mime type to iis?
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument
Doc.ImportFromUrl "http://google.com"
Dim pdffile
pdffile = "test.pdf"
Response.Clear()
response.ContentType = "application/pdf"
response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition", "attachment;filename=" & pdfFile
Doc.SaveHttp "attachment;filename=\aspPDFtemp\"& pdffile&",application/pdf"
Response.Flush()

Open in new window

ajnt__

Does your test page work?

I don't think that adding the MIME type will help at all - I have always used the above approach through code.

CCongdon can you confirm?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
QuestionManA

ASKER
No, the test page doe not work. I receive the same output.
CCongdon

@ajnt___: I agree because I've done this both in ASP and ASP.NET :) As I said, your code should do it, but a couple of embellishments shouldn't hurt :)
 
@QuestionManA: Yes, adding the MIME type to IIS would work IF the client had request http://yourservername.com/pdffile.pdf
But, your client most likely requested something like http://yourservername.com/makepdffile.asp
But you're right. I can't get that code to work either. Let me check on something.
CCongdon

Cool. Well, not cool. The code you posted works in Firefox 3, but not IE7. Let me dig deeper.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CCongdon

Ahhh, I see. The Response.AddHeader content-disposition line needs to be removed. The AspPDF object is outputing those lines as well, and the double header appears to be confusing IE.
ajnt__

Perhaps the issue is using response.clear() then writing a new response stream in PDF.

I've not used response.clear in any of my code in the past but if we don't figure this out in the next 15 minutes I can boot up one of my old systems where I have some code that does exactly this!
ajnt__

OK try the below (taken from http://bytes.com/topic/net/answers/825860-issue-using-binarywrite-ie6-7-a)
Response.Buffer = True
Response.Expires = 0
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Type", "application/pdf")
Response.AddHeader("Content-Length", buffer.Length.ToString)
Response.AddHeader("Content-Disposition", "inline;filename=new.pdf")
Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.BinaryWrite(buffer)
Response.End()

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ajnt__

I defer to my honorable colleague CCongdon's suggestion before trying mine above.
CCongdon

Good call on the caching. Since you don't actually have a 'file' you might confuse caching, so it's best to disable the caching.
I just use .Clear to make sure that IIS didn't send any headers of its own down to the client. A clear slate as it were! :)
The Response.BinaryWrite may or may not work correctly. The object that that QuestionManA is using already has its own output to HTTP stream command...
CCongdon

Well, you got here first, I'm just an assist :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
QuestionManA

ASKER
Intersting thing just happened.
I cleared my ie cache,  and when accessing the page it asked me what I wanted to do with the pdf. Save/Open.
It worked! I'm not sure why/how and I'm still testing but at least it's a start.

The final code is attached.

Thanks to all that helped.

Response.Clear()
response.ContentType = "application/pdf"
response.AddHeader "Content-Type", "application/pdf"
Response.AddHeader "Content-Disposition", "attachment;filename=" & pdfFile
Doc.SaveHttp "attachment;filename=\aspPDFtemp\"& pdffile&",application/pdf"
Response.Flush()

Open in new window

QuestionManA

ASKER
THANK YOU!!