<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/pdfmake.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/vfs_fonts.js"></script>
<div id="test">
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Hello</td>
<td>I</td>
<td>am</td>
</tr>
<tr>
<td>a</td>
<td>table</td>
<td>.</td>
</tr>
</table>
<h1 style="text-align: center">
pdfmake
</h1>
<p>pdfmake does not generate pdfs from the html. Rather, it generates them directly from javascript.</p>
<p>It is very fast, but very limited, especially compared to PHP alternatives.</p>
<p>To get a pdf that looks like the page, you could use html2canvas, which generates an image that can be inserted into the pdf. I think this is a hack and not ideal</p>
</div>
<a href="javascript:generatePDF()">Dowload PDF</a>
function generatePDF() {
var documentDefinition = {
content:
[
{
table:
{
headerRows: 1,
widths: [ '*', '*', '*', '*' ],
body: [
[
{ text: 'Header 1', style: 'tableHeader' },
{ text: 'Header 2', style: 'tableHeader' },
{ text: 'Header 3', style: 'tableHeader' }
],
[
{ text: 'Hello' },
{ text: 'I' },
{ text: 'am' }
],
[
{ text: 'a' },
{ text: 'table' },
{ text: '.' }
],
{
image: 'sampleImage.jpg',
width: 150
},
]
}
},
{
text: 'pdfmake', style: 'header'
},
'pdfmake does not generate pdfs from the html. Rather, it generates them directly from javascript.',
'It is very fast, but very limited, especially compared to PHP alternatives.',
'To get a pdf that looks like the page, you could use html2canvas, which generates an image that can be inserted into the pdf. I think this is a hack and not ideal',
],
styles:
{
header:
{
fontSize: 18,
bold: true,
margin: [0, 10, 0, 10],
alignment: 'center'
},
tableHeader:
{
fillColor: '#4CAF50',
color: 'white'
}
}
};
pdfMake.createPdf(documentDefinition).download('testdoc.pdf');
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.