【发布时间】:2018-04-28 22:06:56
【问题描述】:
当用户单击导出按钮时,我正在使用 jsPDF API 将数据导出到 PDF。当有突出显示的文本或任何表格行中没有值时生成 PDF 时,我遇到了问题。突出显示的文本颜色不会显示在生成的 PDF 中,如果表格包含任何空白值,则表格不会在生成的 PDF 中正确显示。
请找到在线演示https://plnkr.co/edit/GfXDGHWNHh2Mb89In7zK?p=preview
演示:
var app = angular.module("app", []);
app.controller("myController", ["$scope",
function($scope) {
$scope.export = function() {
// var pdf = new jsPDF('landscape');
var pdf = new jsPDF('p','pt','a4');
var pdfName = 'test.pdf';
//var options = {pagesplit: true,'width': 550};
var options = {
pagesplit: true,'width': 500
};
var $divs = $('.myDivClass')
var totalDiv = $divs.length -1;
var currentRecursion=0;
function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
//Once we have done all the divs save the pdf
if(currentRecursion==totalRecursions){
pdf.save(pdfName);
}else{
currentRecursion++;
pdf.addPage();
pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
recursiveAddHtmlAndSave(currentRecursion, totalRecursions)
});
}
}
pdf.fromHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
recursiveAddHtmlAndSave(currentRecursion, totalDiv);
});
}
}
]);
<!doctype html>
<html ng-app="app">
<head>
<link data-require="bootstrap-css@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.33/vfs_fonts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
<script src="split_text_to_size.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="myController">
<button ng-click="export()">Export</button>
<div class="myDivClass" style="background-color:white">
The identity of the longest word in English depends upon the definition of what constitutes a word in the English language, as well as how length should be compared. In addition to words derived naturally from the language's roots (without any known intentional invention), English allows new words to be formed by coinage and construction; place names may be considered words; technical terms may be arbitrarily long. Length may be understood in terms of orthography and number of written letters, or (less commonly)
<font color="red">This is red texttttt</font>
<p><span style='background-color: rgb(255, 255, 0);'>text hightlighted with yellow color</span></p><p><span style='background-color: rgb(255, 0, 0);'>asdjasdjasdsjadhjsahdjasdh
red color</span></p><p><span style='background-color: rgb(255, 0, 0);'><b>asdasdasdasdsaas -- bold and red colorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr</b></span></p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<p><span style="background-color: rgb(255, 255, 0);"><b>Below are the options with bullets</b></span></p><ul><li><span style="background-color: rgb(255, 255, 0);"><b>option1</b></span></li><li><span style="background-color: rgb(255, 255, 0);"><b>option2</b></span></li></ul><p><b style="background-color: rgb(231, 99, 99);">Below are options with numbers</b></p><ol><li><b style="background-color: rgb(231, 99, 99);">option1</b></li><li><b style="background-color: rgb(231, 99, 99);">option2</b></li></ol>
<p><br></p><table class="table table-bordered"><tbody><tr><td>133</td><td><br></td><td><br></td></tr><tr><td><br></td><td>666</td><td><br></td></tr></tbody></table><p><br></p>
</div></div>
</body>
</html>
我确信应该有一些 CSS/js 技巧来解决这个问题。任何输入都是有帮助的。
【问题讨论】:
-
你可以尝试使用html2canvas,然后让jspdf为你生成你的pdf
-
我尝试使用 html2Canvas 但我看到的问题是我无法添加新页面,它总是显示 page1 但如果数据很大则无法添加新页面。@SagarBhattacharya
-
尝试 addHTML 方法,我发布了一个解决方案尝试看看它是否有效
-
html2Canvas 将只生成 1 个页面
标签: javascript jquery css angularjs jspdf