Skip to content

Commit

Permalink
gem wicked pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Apr 24, 2021
1 parent 0c886fb commit c09d4f7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -57,3 +57,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'devise'
gem "faker"
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -207,6 +207,9 @@ GEM
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
wicked_pdf (2.1.0)
activesupport
wkhtmltopdf-binary (0.12.6.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.4.2)
Expand Down Expand Up @@ -234,6 +237,8 @@ DEPENDENCIES
web-console (>= 4.1.0)
webdrivers
webpacker (~> 5.0)
wicked_pdf
wkhtmltopdf-binary

RUBY VERSION
ruby 2.7.2p137
Expand Down
1 change: 0 additions & 1 deletion app/assets/stylesheets/application.css
Expand Up @@ -10,6 +10,5 @@
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
7 changes: 7 additions & 0 deletions app/assets/stylesheets/pdf.scss
@@ -0,0 +1,7 @@
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
table {
width: 100%;
}
12 changes: 12 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -3,9 +3,21 @@ class PostsController < ApplicationController

def index
@posts = Post.all
respond_to do |format|
format.html
format.pdf do
render pdf: "Posts: #{@posts.count}", template: "posts/index.html.erb"
end
end
end

def show
respond_to do |format|
format.html
format.pdf do
render pdf: "Post id: #{@post.id}", template: "posts/post.html.erb"
end
end
end

def new
Expand Down
16 changes: 16 additions & 0 deletions app/views/layouts/pdf.html.erb
@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
<%= wicked_pdf_javascript_include_tag "number_pages" %>
</head>
<body onload='number_pages'>
<div id="header">
<%= wicked_pdf_image_tag 'mysite.jpg' %>
</div>
<div id="content">
<%= yield %>
</div>
</body>
</html>
5 changes: 3 additions & 2 deletions app/views/posts/index.html.erb
@@ -1,7 +1,7 @@
<h1>Posts</h1>

<%= link_to 'New Post', new_post_path %>

<%= link_to "Posts PDF", posts_path(format: :pdf) %>
<br>

<table>
Expand All @@ -10,7 +10,7 @@
<th>Id</th>
<th>Title</th>
<th>Content</th>
<th colspan="3"></th>
<th colspan="4"></th>
</tr>
</thead>

Expand All @@ -20,6 +20,7 @@
<td><%= post.id %></td>
<td><%= post.title %></td>
<td><%= simple_format(post.content) %></td>
<td><%= link_to 'PDF', post_path(post, format: :pdf) %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Expand Down
17 changes: 17 additions & 0 deletions app/views/posts/post.html.erb
@@ -0,0 +1,17 @@
PDF generation date:
<%= Time.zone.now %>

<p>
<strong>Post ID:</strong>
<%= @post.id %>
</p>

<p>
<strong>Title:</strong>
<%= @post.title %>
</p>

<p>
<strong>Content:</strong>
<%= simple_format(@post.content) %>
</p>
1 change: 1 addition & 0 deletions config/initializers/mime_types.rb
Expand Up @@ -2,3 +2,4 @@

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register "application/pdf", :pdf
31 changes: 31 additions & 0 deletions config/initializers/wicked_pdf.rb
@@ -0,0 +1,31 @@
# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md

WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')

# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
layout: "pdf",
orientation: "Portrait",
page_size: "A4"


# Using wkhtmltopdf without an X server can be achieved by enabling the
# 'use_xvfb' flag. This will wrap all wkhtmltopdf commands around the
# 'xvfb-run' command, in order to simulate an X server.
#
# use_xvfb: true,
}

0 comments on commit c09d4f7

Please sign in to comment.