create and print html file using SQL Server Agent job

  • Hi folks,

    after almost a year "SSC abstinence" I'm back with a question:

    Here's the scenario (2008R2):

    The task is to call a stored procedure with some parameter, format the result set as a simple html file and send it to a printer. SSRS or SSIS are not an option, since this would be the only task where we'd use so it would be "oversized", at least at the moment. Sounds easy enough.

    I got to the point where I have the html file created (using bcp in a batch file and a cmdexec job step).

    To print the html file I tried to use a program called printhtml (http://www.printhtml.com/).

    Since I couldn't get it to print from a SQL Server job, I created a simple batch file that calls bcp and after that the printhtml program (code see below).

    When I run the batch from outside SQL Server, everything works fine and the html file will be printed.

    But as soon as SQL Agent is involved, it will just hang forever at the printhtml program (the file will still be created) until I manually cancel the job.

    I added a separate proxy to use my windows credentials instead of the SQL Agent account, but that didn't help either.

    After a while I figured that the printhtml process will start (checked using task manager), but won't print. When killing this process, the SQL Agent job will finish immediately. Further "investigation" lead me to the assumption that this program does something like open the standard web explorer in a silent mode, print the file from there and close the app.

    But the SQL Server Agent service seems to be unable to interact with another program that uses some sort of a GUI. To verify this assumption I changed the batch file and just opened the html file an an IE window instead of trying to print it. The job would still hang until I manually closed the IE tab.

    My next approach was to try a PowerShell job step.

    I tried the sample script based on http://stackoverflow.com/questions/7189299/print-automatically-html-file-with-powershell

    But that didn't work either (disclaimer: my PowerShell experience is limited to the few lines of code I copied...).

    Does anyone have an idea how to accomplish the task without using CLR, SSRS, SSIS? It would be a lot easier, if the application currently calling the sproc would be able to call a PS script, but that's not an option either. The print process needs to be started by the server, not the clients (the printer is installed at the server only).

    Any ideas or hints? Am I hitting in the completely wrong direction?

    TIA

    Lutz

    -- ================================================

    Here's some very basic sample code:

    -- procedure to create a html file

    USE tempdb

    go

    /*

    exec LutzM_printhtml 'some text'

    */

    CREATE PROC dbo.LutzM_printhtml

    (

    @InVariable VARCHAR(10)

    )

    as

    SELECT @InVariable

    FOR xml PATH('test')

    GO

    -- drop proc dbo.LutzM_printhtml

    -- ------------------------------------------------

    batch file (to be called using a cmdexec job step):

    bcp "EXEC tempdb.dbo.LutzM_printhtml 'some text'" queryout c:\temp\file.html -c -T

    c:\temp\printhtml.exe file="c:\temp\file.html"

    REM c:\temp\file.html

    -- ------------------------------------------------



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • The software needs to be registered using your proxy account that is used for the SQL Agent. You know how when you install some new software on the box, it works just fine for you, but when another user logs in, they get prompted with a model dialogue box when they go to use the software for the first time.

    Although I'm not 100% positive on the syntax, from the command prompt try:

    C:\runas /user:YOURDOMAIN\useraccount "C:\ProgramFiles\PrintHtml.exe"

    This executes the application as that user and once the prompts and license information is entered (once) everything works as it should without any prompting.

    Not sure this applies to your scenario but thought I'd share the experience - best of luck!

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • Thank you for your reply. I'll give it a try even though I don't think it will make a difference for two reasons:

    a) the proxy I'm using is based on my Windows domain account, so it "should" be the same and

    b) the program I'm calling doesn't need to be installed. It's a stand alone exe that just needs to be accessible (and that's the case, even when called by the SQL job)

    But you'll never know until you've tried. Right? 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • After spending some more time searching for alternatives I found a software (http://www.coolutils.com/TotalHTMLConverterX) that doesn't need any GUI communication. Seems like it's the solution I've been looking for.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Lutz, ironically enough, that's the exact same piece of software that we use (TotalHTMLConverter) and unfortunately, you will need to use the code I provided in order for it to work unattended - that's where I took the snippet of code from (or last implementation of the software on a new server) 🙂

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • Note, it's only required for the very first time either you or the account that will be used runs it. If you ever change the proxy account you'll need to run it again.

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • I don't think there's anything unfortunate from my point of view, since you provided a solution even before I encoutered the problem! 😀

    So, thank you in advance!!!



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply