+ Post New Thread
Results 1 to 14 of 14
Web Development Thread, Convert HTML / CSS to PDF/PS in Coding and Web Development; Im looking for some kind of server based software which will automate the process of converting a HTML/CSS based web ...
  1. #1

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    29

    Convert HTML / CSS to PDF/PS

    Im looking for some kind of server based software which will automate the process of converting a HTML/CSS based web page to a PDF file.

    The problem i am finding is that many of the scripts - Perl, php etc do not support CSS very well E.g. Floats, inline style and tables

    Obviously I could improve/degrade my HTML and CSS but this is not an option.

    Does anyone have any idea how i might go about this?

    It needs to be an automated process which will allow a bulk job of documents created in HTML/CSS to be converted to PDF on the fly.

    ...

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Not sure what you want to do this on but if you can use Internet Explorer on Windows then you coudl install PDFCreator as a printer driver and then use a script like the one below (make sure the PDFCreator is set as default printer first)

    Basically, it sets a few variables so that the PDF Creator will print without prompting for filenames etc and then it loads each file named in the list at c:\temp\urls.txt (just a plain text file, 1 URL per line). Once the page is loaded it gets printed.

    You don't have any control over things like fitting to page etc (although I suspect you could - this is left as an exercise for the reader :-)) but it basically works.

    Code:
    const OLECMDID_PRINT = 6
    const OLECMDEXECOPT_DONTPROMPTUSER = 2
    const PRINT_WAITFORCOMPLETION =2
    Const HKEY_CURRENT_USER = &H80000001
    
    Initialise
    set oShell=createobject("wscript.shell")
    set OFSO=createobject("scripting.filesystemobject")
    set oFile=ofso.opentextfile("c:\temp\urls.txt")
    do while not ofile.atendofstream
      sURL=ofile.readline
      PrintPage sURL
    loop
    oFile.close
    
    sub PrintPage(sURL)
      Set oIE= CreateObject("InternetExplorer.Application")
      Do While (oIE.Busy)
          Wscript.Sleep 250
      Loop
      oIE.visible=true
      oIE.ToolBar = false
      oIE.StatusBar = false
      oIE.Resizable = false
      oIE.Navigate (sURL)
      Do While (oIE.Busy)
          Wscript.Sleep 250
      Loop
      wscript.sleep 1000 'just so you can see the page
      sFilename=replace(sURL,"/","-")
      sFileName=replace(sURL,":","-")
      oShell.regwrite "HKCU\Software\PDFCreator\Program\AutoSaveFileName",sFileName
      Oie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION,0
      oIe.quit
    end sub
    
    Sub Initialise
      Set oReg = GetObject("winmgmts://./root/default:StdRegProv")
      sPath = "Software\PDFCreator\Program"
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"ShowAnimation",0)
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"UseAutoSave",1)
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"UseAutoSaveDirectory",1)
      lRC=oReg.SetStringValue(HKEY_CURRENT_USER,sPath,"AutoSaveDirectory","c:\temp")
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"AutoSaveStartStandardProgram",0)
    end sub

  3. #3

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    29
    The problem i have is that the pages to be printed are dynamic.

    The system is basically a way of recording staff CPD, with a number of sheets attached.

    The system produces a HTML/CSS template report based on this data.

    I would like 'reviewers' to view the reports on screen but at a certain point in time teacher are not allowed to access the system and make changes, I lock the system at this point. But at the same time I need to output all the reports to PDF on the fly...

    So no files names or URLs just data output to a html page...

    Help please lol

  4. #4

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,618
    Thank Post
    1,230
    Thanked 776 Times in 673 Posts
    Rep Power
    235
    Quote Originally Posted by danIT View Post
    So no files names or URLs just data output to a html page...
    Have you tried XSLT:FO, a transforms language for transforming XML into PDF? Have a look at Apache Xalan, see if that is appropriate to what you are trying to do. I assume you've already had a look at the PDF writing library for whatever language you're developing in? The above idea really doesn't sound bad at all - use a browser engine to render your page, then print that rendered page out to a temporary PDF document - you can easily delete that PDF documents again as soon as you've sent it to the user.

    --
    David Hicks

  5. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Quote Originally Posted by danIT View Post
    The problem i have is that the pages to be printed are dynamic.

    So no files names or URLs just data output to a html page...
    Not sure I understand?? How do you look at the pages now? Do they just come up on a screen as part of an application (ie not rendered in a normal browser window)??

  6. #6

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    29
    Yes they are rendered in a browser window, but dynamically...

    E.g the address for every page generated is http://localhost/report.cgi

    but the contents of each page is different depending on what users report is selected.

  7. #7

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,618
    Thank Post
    1,230
    Thanked 776 Times in 673 Posts
    Rep Power
    235
    Quote Originally Posted by danIT View Post
    E.g the address for every page generated is http://localhost/report.cgi
    You get that your CGI script can simply read a PDF document from disk and write it out as its output? You just have to get your CGI script to set the MIME type correctly (er, application/PDF?), then read in the PDF document from a file on the server and write it out as output. You can generate the PDF document however you like - as you seem to be wanting to generate the PDF document from the rendered HTML view of whatever data it its you're showing, I'd reckon the easiest solution is to have a web browser server-side that you automate in some way (AutoIt?) to automatically "print" to an installed PDF document printer (i.e. PDFCreator).

    --
    David Hicks

  8. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Quote Originally Posted by danIT View Post
    Yes they are rendered in a browser window, but dynamically...

    E.g the address for every page generated is http://localhost/report.cgi

    but the contents of each page is different depending on what users report is selected.
    Still not sure that I understand. I suspect you've actually got to sort 2 things:

    1 - render a report for each possible input
    2 - convert that to PDF

    Assuming that you can't just do something like:

    http://localhost/report.cgi?code=1
    http://localhost/report.cgi?code=2
    http://localhost/report.cgi?code=3

    etc then I think you're stuck.

  9. #9

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    29

    Help

    Can anyone tell me how to go about using the below code? Combile etc....

    or atleast what language this is?

    Code:
    sub PrintPage(sURL)
      Set oIE= CreateObject("InternetExplorer.Application")
      Do While (oIE.Busy)
          Wscript.Sleep 250
      Loop
      oIE.visible=true
      oIE.ToolBar = false
      oIE.StatusBar = false
      oIE.Resizable = false
      oIE.Navigate (sURL)
      Do While (oIE.Busy)
          Wscript.Sleep 250
      Loop
      wscript.sleep 1000 'just so you can see the page
      sFilename=replace(sURL,"/","-")
      sFileName=replace(sURL,":","-")
      oShell.regwrite "HKCU\Software\PDFCreator\Program\AutoSaveFileName",sFileName
      Oie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION,0
      oIe.quit
    end sub
    
    Sub Initialise
      Set oReg = GetObject("winmgmts://./root/default:StdRegProv")
      sPath = "Software\PDFCreator\Program"
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"ShowAnimation",0)
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"UseAutoSave",1)
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"UseAutoSaveDirectory",1)
      lRC=oReg.SetStringValue(HKEY_CURRENT_USER,sPath,"AutoSaveDirectory","c:\temp")
      lRC=oReg.SetDwordValue(HKEY_CURRENT_USER,sPath,"AutoSaveStartStandardProgram",0)
    end sub

  10. #10

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,618
    Thank Post
    1,230
    Thanked 776 Times in 673 Posts
    Rep Power
    235
    Quote Originally Posted by danIT View Post
    Can anyone tell me how to go about using the below code? Compile etc... or at least what language this is?
    Where did the code come from? I'm guessing it's VBScript, intended to be run server-side (i.e. save it with a .vbs extension, run it and see what happens). It looks like it prints a web page for you by starting up an instance of Internet Explorer, waiting until it loads and navigates to a web page, then printing that page to the Open Source version of PDF Creator.

    --
    David Hicks

  11. #11
    kesomir's Avatar
    Join Date
    Mar 2008
    Location
    London
    Posts
    288
    Thank Post
    15
    Thanked 28 Times in 23 Posts
    Rep Power
    17

  12. Thanks to kesomir from:

    danIT (31st July 2008)

  13. #12

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    Quote Originally Posted by dhicks View Post
    Where did the code come from? I'm guessing it's VBScript, intended to be run server-side (i.e. save it with a .vbs extension, run it and see what happens). It looks like it prints a web page for you by starting up an instance of Internet Explorer, waiting until it loads and navigates to a web page, then printing that page to the Open Source version of PDF Creator.

    --
    David Hicks
    Came from me :-)

    It's vbscript - it'll run on any machine as long as you've got internet explorer and a PDF driver installed (actually, it'll print to the default printer but the original request was for PDF)

    as you say, save it as <filename>.vbs Create a text file c:\temp\urls.txt containing addresses of pages you want printing then open a command prompt and type <filename> to get the web pages loaded in turn and then printed.

  14. #13

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,618
    Thank Post
    1,230
    Thanked 776 Times in 673 Posts
    Rep Power
    235
    Quote Originally Posted by srochford View Post
    Came from me :-)
    Eh? <Checks previous posts...> Oh yea! How come the original poster is still having a problem, surely that script does just what he wants?

    --
    David Hicks

  15. #14

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,802
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224

SHARE:
+ Post New Thread

Similar Threads

  1. Wiki Site which teaches html, css and php
    By My220x in forum Coding
    Replies: 1
    Last Post: 4th July 2008, 10:17 PM
  2. html/ javascript help
    By strawberry in forum Coding
    Replies: 2
    Last Post: 2nd July 2008, 11:22 AM
  3. HTML Help
    By Shrimpersfan in forum Windows
    Replies: 6
    Last Post: 25th April 2008, 01:56 PM
  4. HTML Help with Outlook
    By robbied69 in forum Coding
    Replies: 4
    Last Post: 29th February 2008, 04:37 PM
  5. HTML Editor
    By alan-d in forum Web Development
    Replies: 7
    Last Post: 17th April 2007, 07:56 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •