danIT Posted July 15, 2008 Posted July 15, 2008 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. ...
srochford Posted July 15, 2008 Posted July 15, 2008 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. 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
danIT Posted July 16, 2008 Author Posted July 16, 2008 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
dhicks Posted July 16, 2008 Posted July 16, 2008 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
srochford Posted July 17, 2008 Posted July 17, 2008 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)??
danIT Posted July 21, 2008 Author Posted July 21, 2008 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.
dhicks Posted July 21, 2008 Posted July 21, 2008 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
srochford Posted July 21, 2008 Posted July 21, 2008 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.
danIT Posted July 30, 2008 Author Posted July 30, 2008 Can anyone tell me how to go about using the below code? Combile etc.... or atleast what language this is? 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
dhicks Posted July 30, 2008 Posted July 30, 2008 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
kesomir Posted July 30, 2008 Posted July 30, 2008 I feel your pain. html2ps and html2pdf - Convert HTML to PostScript or PDF. Supports over 500 HTML and CSS tags and properties. dompdf - The PHP 5 HTML to PDF Converter FPDF PHP: PDF - Manual Listed a few in case they weren't all on your list. 1
srochford Posted July 31, 2008 Posted July 31, 2008 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 .vbs Create a text file c:\temp\urls.txt containing addresses of pages you want printing then open a command prompt and type to get the web pages loaded in turn and then printed.
dhicks Posted July 31, 2008 Posted July 31, 2008 Came from me :-) Eh? Oh yea! How come the original poster is still having a problem, surely that script does just what he wants? -- David Hicks
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now