Giter Site home page Giter Site logo

Comments (11)

kandadaboggu avatar kandadaboggu commented on August 27, 2024 1

I addressed the issue by monkey patching the pdf_from_string method as follows:

class WickedPdf
  def pdf_from_string(string, options={})
    command = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
    p "*"*15 + command + "*"*15 unless defined?(Rails) and Rails.env != 'development'
    pdf, err = Open3.popen3(command) do |stdin, stdout, stderr|
      stdin.binmode
      stdout.binmode
      stderr.binmode
      stdin.write(string)
      stdin.close
      if block_given?
        success=false
        while(line=stdout.read(1024))
          success = true
          yield line
        end        
        [ ( success ? "SUCCESS" : ""), stderr.read]
      else
        [stdout.read, stderr.read]
      end      
    end
    raise "PDF could not be generated!" if pdf and pdf.rstrip.length == 0
    pdf
  rescue Exception => e
    raise "Failed to execute:\n#{command}\nError: #{e}"
  end
end

This allows me to process the output stream 1024 bytes at a time.

File.open(out_file, 'wb') do |file| 
  WickedPdf.new.pdf_from_string("HELLO") {|line| file << line}
end

This might be an good enhancement to add to the gem.

from wicked_pdf.

unixmonkey avatar unixmonkey commented on August 27, 2024

This is totally doable. In fact, I used to do the same thing (generate pdfs from the model via delayed_job). It's a bit tricky, though. Let me know if something like this works for you:

class Pdfmaker
  def self.generate_pdf(org,items)
     # create instance vars that are needed in the view
     @org = org
     @items = items

     # setup paths
     outfile_path = Rails.root.join('public','uploads','pdfs')
     view_path   = Rails.root.join('app','views','pdfs')

     # parse erb templates
     body = File.read(view_path.join('show.pdf.erb')
     body_render = Erb.new(body).render(binding)
     footer = File.read(view_path.join('_footer.pdf.erb')
     footer_render = Erb.new(body).render(binding)

     # pre-save footer to file for inclusion
     tf = WickedPdfTempfile.new('wicked_pdf_footer_pdf.html')
     tf.write(footer_render)
     tf.flush

     # run through wicked_pdf
     pdf = WickedPdf.new.pdf_from_string(body_render,
       :footer   => { :html => { :url => "file://#{tf.path}" } }, # this line includes the footer we just made
       :margin => { :bottom => 15 } # other options work fine here
     )

     # save final pdf to filesystem
     File.open(outfile_path.join("#{org.id}.pdf"), 'w') {|f| f << pdf }
  end
end

from wicked_pdf.

dgdosen avatar dgdosen commented on August 27, 2024

I'm not that well versed on calling the rendering engine for rails - what do you declare Erb as?

from wicked_pdf.

unixmonkey avatar unixmonkey commented on August 27, 2024

Erb is already declared. It is the .erb rendering library included in Ruby.
Erb.new("hello <%= 'world' %>!").render(binding) #=> "Hello world!"

from wicked_pdf.

dgdosen avatar dgdosen commented on August 27, 2024

Trying to get this to work...
Is the code above for Rails 3.1?

When I try to declare an object of type Erb, I get an uninitialized constant error on Erb. From looking at the source code for rails, I can declare an object as ERB, but that doesn't have a render method. For that I'd need something like an instance of the ActionView...

Any guidance? The only thing I'm blocking on is actually rendering the content from the worker queue

from wicked_pdf.

unixmonkey avatar unixmonkey commented on August 27, 2024

Whoops. I use Haml, so my bits were different and I swapped in ERB from memory.

What you really need is ERB.new(body).result(binding)

from wicked_pdf.

dgdosen avatar dgdosen commented on August 27, 2024

Almost there! -

I can generate PDF's with data, and attach them to an email, but only if I'm not using the wicked_pdf_stylesheet_link_tag in my template. If I try to use it, the template throws an error. undefined method 'wicked_pdf_stylesheet_link_tag' To be clear, I don't get this issue when generating a PDF for consumption in the browser.

Do you know what I'd need to get me some css when generating the doc from the scope of the worker queue?

from wicked_pdf.

unixmonkey avatar unixmonkey commented on August 27, 2024

ActionView Helpers like wicked_pdf_stylesheet_link_tag or even link_to and number_to_currency aren't available in that context. You could instantiate actionview and call render on that as shown here:
#34

Or, you could just avoid using helpers and write out the html you need by hand like so:
<link type='text/css' rel='stylesheet' src='file://<%= Rails.root.join('public','stylesheets','pdf.css').to_s %>' />

from wicked_pdf.

dgdosen avatar dgdosen commented on August 27, 2024

FYI - I couldn't get this to work by manually loading the css file, but I was able to get everything to work by using the method where you create an instance of the ActionView (per #34). When you take that approach, the css file(s) load (using wicked_pdf_stylesheet_link_tag) and you have access to all the view helpers.

from wicked_pdf.

kandadaboggu avatar kandadaboggu commented on August 27, 2024

I am trying to generate the PDF and save it to a file. I am considering using the WickedPdf.new.pdf_from_string technique. I am concerned that this will simply bloat the memory when the PDF generated is large. Is it possible to write the PDF results using some sort of PDF result enumerator

E.g:

File.open("out.pdf", 'wb')  do |file| 
  WickedPdf.new.pdf_from_string(html_string) do | pdf|
    file << pdf
  end
end

from wicked_pdf.

danblaker avatar danblaker commented on August 27, 2024

I added a wiki page https://github.com/mileszs/wicked_pdf/wiki/Background-PDF-creation-via-delayed_job-gem to show a simple example of using delayed_job to generate PDFs in the background.

from wicked_pdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.