I have created a vb.net application (website) that allows the user to fill out a form and save the data to a SQL database, then retrieve the data for later display. The last requirement is to be able to print out a completed form. I am using a Word .dotx template and performing a Word replace to fill out the template. then sending it to the printer. This all works as expected when debugging but when published to IIS on a remote server the print job fails without error. Both my development box and the server have the same version of MS Office installed.
I am doing the replace in the following way:
Dim wordApp As Word.Application = Nothing
Dim document As Word.Document = Nothing
Try
wordApp = New Word.Application() With {.Visible = False}
Dim documentName As String = "SensoryForm.dotx"
Dim projectRoot As String = AppDomain.CurrentDomain.BaseDirectory
Dim wordPath As String = Path.Combine(projectRoot, documentName)
document = wordApp.Documents.Add(Template:=wordPath)
document.Select()
Then using the data to do all the replacing in the Word template. And then trying to print using:
document.Application.ActiveDocument.PrintOut()
'document.Application.ActiveDocument.PrintPreview()
Catch ex As Exception
Finally
If document IsNot Nothing Then
document.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End If
If wordApp IsNot Nothing Then
wordApp.Quit()
End If
End Try
This is my first time trying to print from IIS on a remote server and I don't know what I'm missing. Why does this work when debugging locally but fail without throwing an exception on the server?
It would also be acceptable for the client to automatically download (save) the completed Word document.
Well, your code behind is running on the server. So, any code behind that prints will of course print on THAT computer.
Hence, you have to turn on the printer attached to the web server, and ensure paper is in that printer. Of course that code running on the server has ZERO do to with YOUR computer running some web browser.
So, you have a printer attached to the web server running in the web server room like this:
So, your code behind is running on the computer box, and that computer box is sitting in some web server room. So, when you print, the printing will occur from that web server computer. So, the paper in above picture would come out of the printer sitting in the web server room with the printer attached.
Of course, during development, your web site and server is YOUR computer, so of course it can print to local hardware.
However, a some web server can't touch my files, my hardware, or my printer. If a web server could access my local hardware on my computer, then when you visit my site to view some cute cat pictures? Then I would be able to do things like use your printer, or grab your files called "my banking" or do whatever I want to YOUR computer!
In fact, code behind on a web server can't even tell if I'm using a Android phone, or say some iPad - which of course is unlikely to have a printer attached. So, your code behind can't even know what kind of file system or even if you have say a drive "c:" (a iPad has no such concept).
So, in effect, server side code runs on the the "computer box", and that computer box hosting the web server is going to be in some server room. As result, things like files, printers etc.? They are attached to the computer running the code, and while that computer (the web server) can dish out pages to some browser running on your desktop?
That does not give rise to any code behind on that server being able to do ANYTHING at all with your local hard ware. That includes grabbing files or anything else related to your computer running that browser.
As noted, if any old web site could access YOUR hardware and files on YOUR computer? Then no one would ever risk using the internet due to security concerns.
So, keep in mind that during development you are running the web server and the code behind on YOUR computer, the one computer.
However, the instant you move the web site (and code) to a hosted server, then of course you now only using a browser that can be "send" some content from that web server, not that the web server by some magic act can now use your local hardware, files, and resources like some printer.
The same effect can be seen if you in code behind execute a MessageBox command. That not a browser content, but is a local hardware based msgbox that outputs to the monitor attached to the computer (in this case the web server). Hence, the web server will display the msgbox on the monitor attached to the computer running that code (you would have to enter the server room, find the computer running your web site, and then click "ok" on the monitor attached to the web server. As such, running code behind is running on the hardware and computer that hosts the web site, not on your local desktop computer.
So, yes, you have to create the word document, and then present a button, or file link to download the file. The code behind can also start/trigger a download in most cases. So, you can't create the word document on the client side computer, nor can you print to the client side computer either, since you have no idea if the user is on their Android phone, or their iPad, or using some windows or Mac desktop computer.