Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Generate PDF from HTML using PhantomJS!
Supporting URL, FILE and STRING formats for HTML resource.
Within the Ruby community, there is no simply way to generate PDF from HTML. You must setup dependences separate and then call some wrapped methods around. When you manage several servers or need to do migrations around, things become much more terrible!
PhantomPDF was born to simplify those. It brings simple API and easy maintenance.
Yes, PhantomPDF is simply! You only need to take care of your HTML layout. And then put remaining work to PhantomPDF.
Adding following to your Gemfile
gem 'phantompdf'
and then execute
$ bundle
$ gem install phantompdf
That's all! Pretty simple?!
require 'phantompdf'
options = {
:format => 'A4',
:margin => '1cm'
}
output_file = '/tmp/phantompdf_google.pdf'
# for *URL* resource
url = 'http://www.google.com'
url_generator = PhantomPDF::Generator.new(url, output_file, options)
returned_file = url_generator.generate # If output_file is valid(writable) returned_file == output_file, otherwise returned_file should be temp file of your OS.
# for *FILE* resource
file = 'file://path/to/file.html'
file_generator = PhantomPDF::Generator.new(file, output_file, options)
returned_file = file_generator.generate
# for *STRING* resource
html = '<h1>Hello, PhantomPDF!</h1>'
html_generator = PhantomPDF::Generator.new(html, output_file, options)
returned_file = html_generator.generate
# want to dynamic output?
url_generator.generate('/tmp/dynamic_phantompdf_google.pdf') # This will output pdf file to /tmp/dynamic_phantompdf_google.pdf though you pass *output* arg when creating generator instance.
You can configure PhantomPDF globally in your project, such as Rails' initializers principle.
PhantomPDF.configure do |config|
# default pdf output format, e.g. "5in*7.5in", "10cm*20cm", "A4", "Letter"
:format => 'A4',
# default pdf header, formatted in [headerHeight*]headerString(HTML is supported)
# default to 1.2cm when you omit headerHeight
# you can use *%{pageNum}* and *%{pageTotal}* to refer current values of page
# example: 1.2cm*<h5>PhantomPDF header %{pageNum}/%{pageTotal}
:header => nil,
# default pdf footer, formatted in footerHeight*footerString(HTML is supported)
# you can use *%{pageNum}* and *%{pageTotal}* to refer current values of page
# example: 0.6cm*<h5>PhantomPDF header %{pageNum}/%{pageTotal}
:footer => nil,
# default page margin
:margin => '1cm',
# default page orientation, 'portrait' or 'landscape'
:orientation => 'portrait',
# default page zoom factor
:zoom => 1,
# default cookies, only used for *URL* resource
:cookies => {},
# PhantomJS running timeout
:timeout => 90000,
# pdf rendering timeout, increase if your HTML page is big
:rendering_timeout => 1000
end
You can also configure PhantomPDF in place when needs.
# for *URL* resource
url = 'http://www.google.com'
url_generator = PhantomPDF::Generator.new(url, output_file, options)
url_generator.options.header = '0.8cm*<h3>Configure PhantomPDF dynamic</h3>'
returned_file = url_generator.generate
# create PhantomPDF
PhantomPDF::Generator.new(input, output=nil, options={})
# generate pdf
PhantomPDF::Generator#generate(output=>nil)
# raise PhantomPDF::RenderingError when failed to generate pdf
PhantomPDF::Generator#generate!(output=>nil)
# get generated pdf as string
PhantomPDF::Generator#to_string
If you'd like to help improve PhantomPDF or you find some bugs. You can contribute to it by following:
PhantomPDF is released under MIT license.
FAQs
Unknown package
We found that phantompdf 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.