Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View pdfkit-example.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
##################################################################
# This file uses PDFKit to render a PDF modeled after a Prawn #
# example. It uses RaphaelJS to generate SVG. #
# #
# Note: There seems to be a bug in wkhtmltopdf that is creating #
# an additional page. Seems to be related to `overflow: hidden`. #
# #
# Prawn PDF: http://majesticseacreature.com/prawn-sample.pdf #
# Prawn Source: http://gist.github.com/441285 #
##################################################################
 
html = <<-HTML
<html>
<head>
<style>
html * {
margin: 0;
padding: 0;
}
#canvas {
width: 500px;
height: 500px;
overflow: hidden;
background-color: #CCC;
position: relative;
}
#canvas h1 {
font-family: Courier;
font-size: 80px;
font-weight: bold;
color: #000;
opacity: 0.75;
width: 400px;
height: 100px;
position: absolute;
left: 50px;
top: 210px;
z-index: 5;
text-align: center;
}
#canvas h1 em {
color: #F00;
font-style: normal;
}
#canvas a#repo {
position: absolute;
display: inline-block;
height: 28px;
width: 180px;
color: #FFF;
background-color: #000;
font-family: Arial;
font-size: 14px;
line-height: 28px;
text-align: center;
text-decoration: none;
-webkit-transform: rotate(-90deg);
right: -76px;
top: 236px;
z-index: 10;
}
#flowers {
position: absolute;
top: 0;
left: 0;
z-index: 1;
width: 500px;
height: 500px;
overflow:hidden;
}
#lorem {
color: #000;
font-family: Arial;
font-size: 12px;
line-height: 1.4em;
text-align: justify;
width: 500px;
height: 500px;
overflow:hidden;
}
</style>
</head>
<body>
<div id="canvas">
<h1>PDF<em>Kit</em></h1>
<div id='flowers'></div>
<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed magna.</p
<a id="repo" href="http://github.com/jdpace/pdfkit">github.com/jdpace/pdfkit</a>
</div>
<script src="http://github.com/DmitryBaranovskiy/raphael/blob/master/raphael-min.js?raw=true&1ljkj" type="text/javascript"></script>
<script type="text/javascript">
function flower(paper, x, y) {
var flower = paper.set();
flower.push(
petal(0,x,y,paper),
petal(90,x,y,paper),
petal(180,x,y,paper),
petal(270,x,y,paper)
);
flower.rotate(45)
return(flower)
}
function petal(angle, x, y, paper) {
var side = paper.width / 10;
var triangle = paper.path("M"+x+" "+y+"L"+(x+side)+" "+(y+side)+"L"+(x+(side*2))+" "+y+"Z");
triangle.attr({fill: randomColor()});
triangle.rotate(angle,x,y);
}
function stringOfFlowers(paper,x,y,dir) {
for(i=1;i<=4;i++) {
var offset = paper.width / 5;
var coX = x + (offset * i);
var coY = y + (offset * i * dir);
flower(paper,coX,coY);
}
}
function randomColor() {
return('rgb('+Math.floor(Math.random() * 255)+','+Math.floor(Math.random() * 255)+','+Math.floor(Math.random() * 255)+')');
}
var paper = Raphael('flowers', 500, 500);
stringOfFlowers(paper, 0, 0, +1);
stringOfFlowers(paper, 0, 500, -1);
</script>
</body>
</html>
HTML
 
require 'rubygems'
require 'pdfkit'
 
kit = PDFKit.new(html, :page_width => '500px', :page_height => '500px',
:margin_top => 0, :margin_bottom => 0, :margin_left => 0, :margin_right => 0,
:dpi => 300)
 
path = File.join(ENV['HOME'],'pdfkit-example.pdf')
kit.to_file(path)
 
puts "Saved example PDF to #{path}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.