Comments (11)
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.
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.
I'm not that well versed on calling the rendering engine for rails - what do you declare Erb as?
from wicked_pdf.
Erb is already declared. It is the .erb rendering library included in Ruby.
Erb.new("hello <%= 'world' %>!").render(binding) #=> "Hello world!"
from wicked_pdf.
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.
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.
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.
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.
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.
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.
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)
- Add support for --load-error-handling and --load-media-error-handling
- Remote asset returning non-200 response does not trigger an error
- Greek Character "𝛼" not showing correctly within PDF generated by Wicked PDF HOT 7
- Images won't load from Active Storage using Amazon S3 Bucket HOT 2
- version 2.7.0 breaks with webpacker
- Content is getting cropped in PDF
- ActionView::Template::Error uninitialized constant WickedPdf::WickedPdfHelper::Assets::SprocketsEnvironment::Sprockets HOT 3
- Feature Request: PDF/A-1
- pdf_from_url CSS not working HOT 1
- Raw PDF being rendered. HOT 1
- OpenStruct causes NameError in Rake13.2.0
- PDF is being rendered with raw ERB code HOT 4
- Content overlaps with the header layout
- RuntimeError on PDf Generation HOT 1
- wicked_pdf due to wkhtmltopdf not finding OpenSSL 1.1 despite installing over OpenSSL 3 HOT 7
- Remove usage of `OpenStruct` or add `ostruct` as dependency
- v2.8.0 causing ActionView::Template::Error: no implicit conversion of nil into String for first time in years HOT 7
- ArgumentError when changing the WickedPdf.config={...} to the new WickedPdf.configure.do |c| ... end HOT 1
- Error: PDF could not be generated! HOT 1
- Public assets not getting loaded after upgrade to Rails 7 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wicked_pdf.