Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpalumickas/wkhtmltopdf_runner
Ruby wrapper for wkhtmltopdf to render PDF from HTML
https://github.com/jpalumickas/wkhtmltopdf_runner
hacktoberfest htmltopdf pdf pdf-generation rails ruby ruby-on-rails wkhtmltopdf
Last synced: about 5 hours ago
JSON representation
Ruby wrapper for wkhtmltopdf to render PDF from HTML
- Host: GitHub
- URL: https://github.com/jpalumickas/wkhtmltopdf_runner
- Owner: jpalumickas
- License: mit
- Created: 2019-02-19T00:03:23.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-02T00:25:44.000Z (about 2 months ago)
- Last Synced: 2024-09-13T17:13:42.429Z (11 days ago)
- Topics: hacktoberfest, htmltopdf, pdf, pdf-generation, rails, ruby, ruby-on-rails, wkhtmltopdf
- Language: Ruby
- Homepage:
- Size: 42 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Wkhtmltopdf Runner
This gem is a wrapper for a popular `wkhtmltopdf` library to generate PDF files from HTML.
## Installation
Add this line to your application's Gemfile and then run `bundle install`:
```ruby
gem 'wkhtmltopdf_runner'
```You also need to have installed `wkhtmltopdf`. Easy way to do that is to add binary files to your Gemfile.
```ruby
gem 'wkhtmltopdf-binary' # or 'wkhtmltopdf-binary-edge'
```If you have custom path for `wkhtmltopdf` please see [Configuration](#configuration)
## Usage
### Write to File
Use in a block which has File argument that will be returned after PDF generation as a TempFile.
#### ActiveStorage in Rails
These examples show how to attach a file to a user with [ActiveStorage::Attach](https://api.rubyonrails.org/classes/ActiveStorage/Attached/One.html).
You can render PDF from HTML string:```rb
string = 'Hello user
'WkhtmltopdfRunner.pdf_from_string(string) do |file|
user.document.attach(io: file, file_name: 'document.pdf')
end
```You can render PDF from HTML file:
```rb
File.open('index.html') do |html_file|
WkhtmltopdfRunner.pdf_from_file(html_file) do |file|
user.document.attach(io: file, file_name: 'document.pdf')
end
end
```You can render PDF from URL:
```rb
WkhtmltopdfRunner.pdf_from_url('https://github.com') do |file|
user.document.attach(io: file, file_name: 'document.pdf')
end
```
### SinatraYou can send the resultant file as a download.
```rb
WkhtmltopdfRunner.pdf_from_url('https://github.com') do |file|
send_file file, { filename: 'document.pdf' }
end
```### Save to file
You can also just save it to a file.
```rb
WkhtmltopdfRunner.pdf_from_url('https://github.com') do |file|
doc = File.open('document.pdf', 'w')
doc.write(file.read)
doc.close
end
```
### Render to StringIf you will not provide block, PDF string will be returned.
> **Warning:** Be careful when using this method because all PDF content will
> be stored in Memory. Better to use block which will return file. See examples
> above.```rb
pdf_string = WkhtmltopdfRunner.pdf_from_url('https://github.com')
```## Configuration
If you're using Rails, create file in `config/initializers/wkhtmltopdf_runner.rb`
```rb
WkhtmltopdfRunner.configure do |config|
config.debug = true # Default: false
config.logger = Logger.new('runner.log') # Default: STDOUT or Rails.logger in Rails
config.binary_path = '/path/to/wkhtmltopdf' # Default: will search automatically in PATH or Gemfile
end
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jpalumickas/wkhtmltopdf_runner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## Code of Conduct
Everyone interacting in the WkhtmltopdfRunner project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jpalumickas/wkhtmltopdf_runner/blob/master/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).