Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Create PDF documents in Rails your app from HTML, CSS and JS.
Features:
It's uses ReLaXedJS tool, which is wrapper arround chromium headless.
The idea of this gem is to separate logic of PDF creation from regular Rails views/controllers. Make it independent and easy to maintain.
If you want to contribute and add more templates - it' very easy to do. See #Templates section of this doc.
Template: simple_invoice | Template: basic_invoice | Template: chart1 | |
Template: products |
You can use predefined starter templates (and you are welcome to contribute and create additional templates):
Use template starters:
rails g rails_pdf new invoice_report
(create blank template for PDF)rails g rails_pdf basic_invoice report
rails g rails_pdf chart1 report
rails g rails_pdf simple_invoice report
After you've generated PDF template, you can edit it in app/pdf/<folder>/<file>
file.
You can use JS/CSS files from app/pdf/shared
(which includes bootstrap 4, foundation 6, Found Awesome 5, Charts.js).
This is how you can generate and send PDF files on the fly:
def report
RailsPDF.template("report2/invoice.pug.erb").render do |data|
send_data(data, type: 'application/pdf', disposition: 'inline', filename: 'report.pdf')
end
end
# or return file as attachment
def invoice
RailsPDF.template("report2/invoice.pug.erb").render do |data|
send_data(data, type: 'application/pdf', disposition: 'attachment', filename: 'report.pdf')
end
end
# sample with locals
# works similar how regular partials works
def report
@invoice = Invoice.find(params[:id])
RailsPDF.template("report2/invoice.pug.erb").locals(invoice: @invoice).render do |data|
send_data(data, type: 'application/pdf', disposition: 'inline', filename: 'report.pdf')
end
end
If you need to create PDF file and save to file on drive:
RailsPDF.template("report/chart.pug.erb").render_to_file('path/docs/report.pdf') # File
# or for html template
RailsPDF.template("sales/invoice.html.erb").render_to_file('path/docs/report.pdf') # File
Same but save PDF into Temfile:
RailsPDF.template("report/chart.pug.erb").render_to_tempfile('report.pdf') # Tempfile
With ERB files you can use App code (like models, etc). For example you can iterate over @users and output in PDF.
Basically you need to put an absolute path to asset or remote URL (for example on CDN. but local files works faster).
style
include:scss <%= Rails.root %>/app/pdf/report/stylesheets/invoice.scss
img(src="<%= Rails.root %>/app/pdf/shared/images/rails_pdf.png")
script(src='<%= Rails.root %>/app/pdf/shared/javascripts/Chart.bundle.min.js')
Installation of gem is very simple, it's just requires one additional step to install RelaxedJS tool which is using Chrome headless.
relaxed --version
)$ git clone https://github.com/RelaxedJS/ReLaXed.git .
$ npm install
$ sudo npm link --unsafe-perm=true
Verify it's installed with: relaxed --version
.
gem 'rails_pdf'
And then execute:
$ bundle
div(style="page-break-before:always")
devicePixelRatio: 3,
// A4
$page-width: 8.27in;
$page-height: 11.69in;
h1 My document
p some paragraph
template#page-header
p I appear at the top of the page
template#page-footer
p I appear at the bottom of the page
test/dummy
bundle exec rake db:migrate
bundle exec rails s -b 0.0.0.0
localhost:3000/report.pdf
lib/generators/rails_pdf/templates
and add folder with template (html,css,js)templates/shared
folderlib/generators/rails_pdf/rails_pdf_generator.rb
add new type of reportdocs
folderRailsPDF.url("http://google.com").render_to_file("google.com.pdf")
Before deploy app to production please don't forget to install Relaxed.JS on it.
You are welcome to contribute.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that rails_pdf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.