Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS to send PDF output to Printer INSTEAD of displaying in IE ? 1

Status
Not open for further replies.

mb22

Programmer
Sep 4, 2002
258
US
I use AvtiveReports .Net to generate a PDF document which I can preview in the browser. No problem!

Problem, I want to send the document DIRECTLY to the printer without having to preview it. How do I do it?

Response.ContentType = "application/pdf"

' IE & Acrobat seam to require "content-disposition" header being in the response. If you don't add it, the doc still works most of the time, but not always.
' this makes a new window appear: Response.AddHeader("content-disposition","attachment; filename=MyPDF.PDF")
Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF")


' Create the PDF export object
Dim pdf As PdfExport = New PdfExport()
' Create a new memory stream that will hold the pdf output
Dim memStream As System.IO.MemoryStream = New System.IO.MemoryStream()
' Export the report to PDF:
pdf.Export(rpt.Document, memStream)
' Write the PDF stream out
Response.BinaryWrite(memStream.ToArray())
' Send all buffered content to the client
Response.End()

 
You could embed the pdf in an HTML document and invoke a print method on it.
Code:
<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden>
<a onClick="document.getElementById('Pdf1').printWithDialog()" style="cursor:hand;">Print file</a>
or
Code:
<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">
     <PARAM NAME='SRC' VALUE="file.pdf">
</OBJECT>
<a onClick="document.Pdf2.printWithDialog()">Print file</a>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
adam0101:
thanks alot. I'm really pursuing your direction. 2 questions!!

1.printWithDialog() ..... suppose I do not want to see the printer Dialog ... just print! What is the funtion? I have tried

document.Pdf2.print() ... still brings up print dialog!
document.Pdf2.printWithoutDialog() ... does not work!

2. This may be the biggie ..perhaps 3 STARS!
I create the pdf file dynamically ... so the name of the file is different for each user. user1.pdf user2.pdf user3.pdf ..... when user1,user2,user3 log on respectively. I tried to set the <OBJECT> element to runat=server ...but I did not see the ID in the ASP.Net IDE to reference it and set the "SRC" param value dynamically ...

I think that is the best shot??? .... what will be the namespace for the <OBJECT> or how will I create the reference in order to be able to do that. Or do you have other ideas?
 
I had another idea which I think you can help me on. Suppose I added a hidden field "txtFileName" and put the file name in that hidden field so that I can then dynamically read it with JS. What will be the JS to set the SRC value

i tried
<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">

<PARAM NAME="SRC" value=document.all.txtFileName.value >

</OBJECT>
but it didn't work .... Is there a way to assign the value in JS?
 
Still researching in the forum .... Can I use document.write to write it out? with script tags in the body section where this object is defined?

<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">

<PARAM NAME="SRC" value=document.all.txtFileName.value >

</OBJECT>

Any help?

 
That's what I was thinking, either that or maybe something like this?
Code:
<script>
var pdfDoc='<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">'+
'<PARAM NAME='SRC' VALUE="'+document.yourFormName.txtFileName.value+'"></OBJECT>'
document.getElementById("divPDF").innerHTML=pdfDoc;
</script>
Code:
<div id="divPDF"></div>
Or maybe use document.write in a hidden IFRAME?

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
adam0101:
Can you check the syntax? ... I've tried it but its not printing. Does this need to change with the div element now?

<a onClick="document.Pdf2.printWithDialog()">Print file</a>

Or how would you use IFRAME .. I have not done that before
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top