以下是代码。
<html>
<head>
<script src="jquery-1.12.0.js"></script>
<script src="jspdf.min.js"></script>
<style type="text/css">
.div1{
border-width: 1px;
border-style: solid;
border-color: black;
background-color: red;
text-align: center;
height: 500px;
}
</style>
</head>
<body>
<div id="content">
<div class="div1">
this is the div with some css
</div>
</div>
<br/>
<button id="cmd">generate PDF</button>
<script type="text/javascript">
$(document).ready(function(){
var doc = new jsPDF();
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
$('#cmd').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});
});
</script>
</body>
</html>