How to generate pdf file from an html page??

A

abhishek

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Thank you All
 
Z

Zentrader

Sorry, I read that backwards. I do it the opposite of you. Anyway a
google for "html to pdf python" turns up a lot of hits. Again, no
reason to reinvent the wheel.
 
R

Ramsey Nasser

Sorry, I read that backwards. I do it the opposite of you. Anyway a
google for "html to pdf python" turns up a lot of hits. Again, no
reason to reinvent the wheel.

Like Zentrader said, theres no reason to reinvent the wheel. An HTML
to PDF converter is no trivial task. You would essentially have to
implement an HTML layout engine that outputs PDF files. Not only does
that mean you would have to programatically produce a PDF file, but it
means you would have to parse and correctly render HTML and CSS
according to accepted web standards, the W3C's specifications. This
has proved difficult to do and get right in practice, as is evident
from the browser compatibility issues that continue to plague the web.

Theres a package called Prince that's supposed to do an excellent job.
Check it out:

http://www.princexml.com/

Its layout engine surpasses some browsers in terms of compatibility
with web standards. I don't think its free for commercial use, though,
so this might depend on what exactly you're trying to do.

An alternative idea it to wait for Firefox 3 to come out. If I'm not
mistaken, it will feature a new version of the Gecko layout engine
which will use Cairo for all its rendering. Coincidently, Cairo can be
made to output PDF files. So, you may be able to hack something
together.
 
S

Shane Geiger

Just some thoughts to get you started:

You may not get any responses because you weren't specific enough about
what you want to do. Since you are asking about doing this via Python,
it seems you want to automate something which can be done from a menu
option in various Web browsers (use the print feature and print to
PDF). You could, of course, download the files (as with the
command-line Web client, wget) and then convert html to PDF using
various tools. Of course, this gives you a different result--of
course--because you would be using a different HTML rendering engine.
So you have to ask yourself: Is your goal to have a page that looks
exactly like it looks in Firefox? or in IE? or Safari? Or are you only
concerned that you have the words of the document?

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Thank you All


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
W

Waldemar Osuch

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Thank you All

You may want to investigate.
http://pisa.spirito.de/
It worked for me in some simple conversions
 
G

Grant Edwards

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Here's one way:

------------------------------html2pdf.py----------------------------------------
#!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))
 
A

abhishek

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Here's one way:

------------------------------html2pdf.py-----------------------------------------
#!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))

hi grant have tried the command it resulted in the following errors
--

sh: a2ps: not found
ESP Ghostscript 815.04: **** Could not open the file /home/samba/users/
Abhishek/newTemplate.pdf .
**** Unable to open the initial device, quitting.
256
 
G

Grant Edwards

Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Here's one way:

------------------------------html2pdf.py-----------------------------------------
#!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))
----------------------------------------------------------------------------------
hi grant have tried the command it resulted in the following errors

sh: a2ps: not found

You'll need to install a2ps. It's available as a standard
package for all the distros I've ever used.
ESP Ghostscript 815.04: **** Could not open the file /home/samba/users/
Abhishek/newTemplate.pdf .
**** Unable to open the initial device, quitting.
256

Either your ghostscript installation is broken, or you've tried
to use an output path/file that's not writable. I suspect the
latter.
 
T

Terry Jones

Grant" == Grant Edwards said:
Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Here's one way:

------------------------------html2pdf.py-----------------------------------------
#!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))

Note that this is highly insecure. outputFilename could be passed e.g., as

/tmp/file.pdf; rm -fr /home/abhishek

Terry
 
I

Ismail Dönmez

Wednesday 19 December 2007 17:40:17 tarihinde Terry Jones şunları yazmıştı:
Grant> On 2007-12-19 said:
Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why
it cant be done.

Here's one way:

------------------------------html2pdf.py------------------------------
----------- #!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" %
(inputFilename,outputFilename))

Note that this is highly insecure. outputFilename could be passed e.g., as

/tmp/file.pdf; rm -fr /home/abhishek

And the solution is to use subprocess [0] instead of os.system()

[0] http://docs.python.org/lib/module-subprocess.html

Regards,
ismail
 
G

Grant Edwards

Grant> On 2007-12-19 said:
Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.

Here's one way:

------------------------------html2pdf.py-----------------------------------------
#!/usr/bin/python
import os,sys

inputFilename,outputFilename = sys.argv[1:3]

os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))

Note that this is highly insecure. outputFilename could be passed e.g., as

/tmp/file.pdf; rm -fr /home/abhishek

Here's a half-assed solution:

inputFilename = inputFilename.replace("'","")
outputFilename = outputFilename.replace("'","")

os.system("w3m -dump '%s' | a2ps -B --borders=no | ps2pdf - '%s'" % (inputFilename,outputFilename))

As somebody else suggested, building the pipeline "by hand"
using the subprocess module is the most bullet-proof method.
 
M

MonkeeSage

"Grant" == Grant Edwards <[email protected]> writes:
Grant> On 2007-12-19 said:
Hi everyone, I am trying to generate a PDF printable format file from
an html page. Is there a way to do this using python. If yes then
which library and functions are required and if no then reasons why it
cant be done.
Here's one way:
------------------------------html2pdf.py-----------------------------------------
#!/usr/bin/python
import os,sys
inputFilename,outputFilename = sys.argv[1:3]
os.system("w3m -dump %s | a2ps -B --borders=no | ps2pdf - %s" % (inputFilename,outputFilename))
Note that this is highly insecure. outputFilename could be passed e.g., as
/tmp/file.pdf; rm -fr /home/abhishek

Here's a half-assed solution:

inputFilename = inputFilename.replace("'","")
outputFilename = outputFilename.replace("'","")

os.system("w3m -dump '%s' | a2ps -B --borders=no | ps2pdf - '%s'" % (inputFilename,outputFilename))

As somebody else suggested, building the pipeline "by hand"
using the subprocess module is the most bullet-proof method.

This looks a little better for me ... | a2ps -B --borders=0 --
columns=1 -f 10.0 | ...

Regards,
Jordan
 
G

Grant Edwards

This looks a little better for me ... | a2ps -B --borders=0 --
columns=1 -f 10.0 | ...

Right. I forgot that I've adjusted my a2ps defaults to using a
single column and a readable font size instead of the standard
2-up tiny-font mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top