Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-03-2009
Member
 
Join Date: Oct 2005
Posts: 144
HTML and CSS to PDF

I have been researching and tearing my hair for several days on how to convert a html file containing pdf and css with php. I tried FDPF (I do not know how to pass a parameter to html page), but this book did not seem to handle the css, I also tried dompdf and html2pdf but nothing worked for me. If you have any ideas then please let me know. Thank you
Reply With Quote
  #2  
Old 18-03-2009
Member
 
Join Date: Dec 2007
Posts: 1,592
Re: HTML and CSS to PDF

Prince is a command line program, available for whatever platform you?re probably running.

It can take a variety of inputs including files on disk, or even HTTP urls.

Here is a very simple Ruby library to call Prince and a helper module to include on my Rails controllers. I store them both in the lib folder of my Rails application.

Here?s the full helper, slightly modified for simplicity:

Code:
# We use this chunk of controller code all over to generate PDF files.
#
# To stay DRY we placed it here instead of repeating it all over the place.
#
module PdfHelper
  require 'prince'

  private
    # Makes a pdf, returns it as data...
    def make_pdf(template_path, pdf_name, landscape=false)
      prince = Prince.new()
      # Sets style sheets on PDF renderer.
      prince.add_style_sheets(
        "#{RAILS_ROOT}/public/stylesheets/application.css",
        "#{RAILS_ROOT}/public/stylesheets/print.css",
        "#{RAILS_ROOT}/public/stylesheets/prince.css"
      )
      prince.add_style_sheets("#{RAILS_ROOT}/public/stylesheets/prince_landscape.css") if landscape
      # Render the estimate to a big html string.
      # Set RAILS_ASSET_ID to blank string or rails appends some time after
      # to prevent file caching, ****ing up local - disk requests.
      ENV["RAILS_ASSET_ID"] = ''
      html_string = render_to_string(:template => template_path, :layout => 'document')
      # Make all paths relative, on disk paths...
      html_string.gsub!("src=\"", "src=\"#{RAILS_ROOT}/public")
      # Send the generated PDF file from our html string.
      return prince.pdf_from_string(html_string)
    end

    # Makes and sends a pdf to the browser
    #
    def make_and_send_pdf(template_path, pdf_name, landscape=false)
      send_data(
        make_pdf(template_path, pdf_name, landscape),
        :filename => pdf_name,
        :type => 'application/pdf'
      ) 
    end
end
This simple module has two methods. Both take an ERB template path as an argument, then a pdf file name.

Make_pdf renders the template to a string, then does some modifications to make all requests within local.

When it?s done it returns the PDF file as data. Nothing is rendered to disk. This method is useful for not only sending the PDF file to the client (as in make_and_send_pdf), but when generating PDF files for email attachments.
Reply With Quote
  #3  
Old 18-03-2009
Member
 
Join Date: Dec 2007
Posts: 1,542
Re: HTML and CSS to PDF

Download Python HTML/CSS to PDF converter

Features
  • Translates HTML and CSS input into PDF files
  • Is written pure Python and therefore platform independent
  • Supports document specifics like columns, headers, footers, page numbers, custom Postscript and TrueType fonts, etc.
  • Best support for frameworks like Django, Turbogears, CherryPy, Pylons, WSGI
  • Simple integration into Python programms
  • Also available as stand alone command line tool for Windows, MacOS X and Linux
Reply With Quote
  #4  
Old 18-03-2009
Member
 
Join Date: Dec 2007
Posts: 2,273
Re: HTML and CSS to PDF

You can also check out this: How to convert htm (or html) pages into PDF documents ?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "HTML and CSS to PDF"
Thread Thread Starter Forum Replies Last Post
html help saffu khan Software Development 3 03-05-2010 01:01 PM
XML vs HTML technika Software Development 5 22-01-2010 09:14 AM
HTML Frames Adrina_g Software Development 5 19-01-2010 10:06 AM
How to define HTML Validator and services provided by HTML Validator Aldous Software Development 3 22-09-2009 01:12 PM
How do I get the HTML code to display as HTML code? NAYASA Software Development 3 26-12-2008 01:35 PM


All times are GMT +5.5. The time now is 02:08 AM.