-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack helpers does not work #860
Comments
Cross-referencing your StackOverflow post here: There's no answer there. Did you ever figure this out or find a workaround? |
No, I did not find a workaround. If anyone has a clue, he is welcomed! |
https://stackoverflow.com/a/60541688/538534 AnalysisThe webpack helpers make several assumptions that might not hold in every project. They produce two different results depending on what running_in_develpment? returns. With webpacker 3.0.0 or newer this method delegates to Without the dev server running the helpers will assume the assets were precompiled and will attempt to paste the contents of the asset into a With the dev server running the webpack helpers will return a tag with an asset path to the pack asset (eventually using standard
Additional constraintsIn our case we had some additional constraints:
Sample solutionOur solution has been to implement our own helpers that are more aware of our set up.
module PdfHelper
def pdf_stylesheet_pack_tag(source)
if running_in_development?
options = { media: "all" }
wds = Webpacker.dev_server
options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
stylesheet_pack_tag(source, options)
else
wicked_pdf_stylesheet_pack_tag(source)
end
end
def pdf_javascript_pack_tag(source)
if running_in_development?
options = {}
wds = Webpacker.dev_server
options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
javascript_pack_tag(source, options)
else
wicked_pdf_javascript_pack_tag(source)
end
end
end In the pdf layout (slim) html
head
...
= pdf_stylesheet_pack_tag "pdf"
= pdf_javascript_pack_tag "pdf"
... |
I already commented in the stackoverflow thread but for completeness I'll restate my comment here: The solution of @artm is working perfectly for me except for the fact that the method I did this by just defining it in the PdfHelper itself as def show_as_html?
params[:debug].present?
end This may not be the best solution but it basically works for the moment. Another thing I came about is that using the custom helpers won't work when creating the pdf outside of the ActionController context. E.g. when doing pdf_html = ActionController::Base.new.render_to_string(tamplate: "my_pdf_template.pdf", layout: "pdf")
pdf = WickedPdf.new.pdf_from_string(pdf_html) I am running in undefined method Any suggestions? |
The instantiation of a If you really wanted to make what you have work, you'd have to do something more like this: renderer = ActionController::Base.new
renderer.extend(ApplicationHelper) # add helper methods
renderer.extend(Rails.application.routes.url_helpers) # add route helpers for link_to and such
pdf_html = renderer.render_to_string(tamplate: "my_pdf_template.pdf", layout: "pdf") Rails now has a version of pdf_html = ApplicationController.render(
template: 'my_pdf_template',
format: :pdf,
layout: 'pdf',
assigns: { users: @users }
) Please let me know if that helps or not! |
This helped a lot. Thank you! |
Hi @basiszwo, Thank you for the detailed explanation of the problem + solution. A little over a year and a half later, I'm finding this solution is not completely resolving the css loading issue. My pdf loads, but without any styles; any help would be greatly appreciated. Here's my setup: webacker 6.0.0.beta.7 // app/javascript/packs/pdf.js
import "stylesheets/pdf.scss" /* app/javascript/stylesheets/pdf.scss */
@import "tailwindcss/base" <!-- app/views/layouts/pdf.html.erb -->
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<%= pdf_stylesheet_pack_tag "pdf" %>
</head>
<body onload="number_pages">
<div id="header">
<!--= wicked_pdf_image_tag 'thumbnail.png', height: "30", width: "auto"-->
</div>
<div id="content">
<%= yield %>
</div>
</body>
</html> The original helpers included a method
For the sake of progress I'm checking Rails, module ApplicationHelper
def pdf_stylesheet_pack_tag(source)
if Rails.env.development?
options = { media: "all" }
wds = Webpacker.dev_server
options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
stylesheet_pack_tag(source, options)
else
wicked_pdf_stylesheet_pack_tag(source)
end
end
def pdf_javascript_pack_tag(source)
if Rails.env.development?
options = {}
wds = Webpacker.dev_server
options[:host] = "#{wds.host}:#{wds.port}" unless show_as_html?
javascript_pack_tag(source, options)
else
wicked_pdf_javascript_pack_tag(source)
end
end
def show_as_html?
params[:debug].present?
end
def running_in_development?
Webpacker.dev_server.running?
end
end Output: Update I realized my webpack was hosted on 8080, even though Webpacker outputs 3035. Adding
Seems like we're almost there, but I'm at a loss as to why colors won't render. |
I'm serving assets/packs with Rails (behind a CDN in production) and I'm quite happy with my setup. So, after encountering this (at all the latest components' versions at the time of writing) and not being satisfied with the suggested workarounds of the already convoluted functionality, I've come up with this: I hope this is useful and will inspire some rework of the gem — I believe a simple task like "pass input to wkhtmltopdf and return output" can be implemented in a much, much simpler way. However, unfortunately, I can't even recommend this too much since |
Hello @mrjonesbot : I'm exactly in your situation (no color, no grid) if I use TailwindCSS. Did you find a solution ? Thanks a lot. |
@omontigny If your stylesheet uses too-modern selectors from CSS 3.0, and probably 2.1, |
@omontigny I switched to Grover, which takes a slightly different approach and depends on puppeteer. I've found it to be great since I can use Tailwind without issues. |
@mrjonesbot How did you use Tailwind in Grover? My pdf made by grover can not show the CSS style from tailwind, but the tailwind on html is working. What do I miss when I use grover?!
|
Try adding |
Issue description
In wicked_pdf doc, it is said that I can use
wicked_pdf_stylesheet_pack_tag
andwicked_pdf_javascript_pack_tag
to include my stylesheets and javascript from webpack but nothing works.In a rails project with webpack, here is the code from the controller:
Here is the code from the layout:
Here is the code from the view pdf.erb:
Expected or desired behavior
It works with wicked_pdf_stylesheet_link_tag (test-wicked is applied from sprockets: text is blue) but not with wicked_pdf_stylesheet_pack_tag (h1 should be red but is not).
PS: I posted originally the question on stackoverflow but I think it makes more sens to address an issue.
System specifications
wicked_pdf gem version (output of
cat Gemfile.lock | grep wicked_pdf
): 1.4.0wkhtmltopdf version (output of
wkhtmltopdf --version
): 0.12.4whtmltopdf provider gem and version if one is used:
wkhtmltopdf-binary (0.12.4)
platform/distribution and version (e.g. Windows 10 / Ubuntu 16.04 / Heroku cedar):
Ubuntu 18.04
Thank you in advance!
The text was updated successfully, but these errors were encountered: