生成一个PDF文件,保留HTML页面的样式,使用jsPDF实现。

7

我正在尝试创建一个按钮,以Sass样式呈现的PDF将自动下载该页面。但是,无论我尝试什么,都会导致样式混乱。

这是页面(这是一个具有多种不同内容类型的测试站点):

http://gobrandgotests.wpengine.com/preview-pdf/

但是PDF看起来像这样:

enter image description here

我正在调用 jspdf.debug.js 并在我的页面上使用以下HTML按钮+脚本:

<div id="bypass"> <!-- keeps button from showing in PDF -->
    <button id="pdf-new" style="margin: 50px;"><a href="javascript:demoFromHTML()" class="button" style="color: black;">Generate PDF</a></button>
</div>

<script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        // source can be HTML-formatted string, or a reference
        // to an actual DOM element from which the text will be scraped.
        source = $('#content')[0];

        // we support special element handlers. Register them with jQuery-style 
        // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
        // There is no support for any other type of selectors 
        // (class, of compound) at this time.
        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypass': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        // all coords and widths are in jsPDF instance's declared units
        // 'inches' in this case
        pdf.fromHTML(
        source, // HTML string or DOM elem ref.
        margins.left, // x coord
        margins.top, { // y coord
            'width': margins.width, // max width of content on PDF
            'elementHandlers': specialElementHandlers
        },

        function (dispose) {
            // dispose: object with X, Y of the last line add to the PDF 
            //          this allow the insertion of new lines after html
            pdf.save('Test.pdf');
        }, margins);
    }
</script>

如何使样式从HTML到PDF保持一致?

有一个名为pd4ml的Java库,它专门为此功能而设计。http://pd4ml.com/ - QuestionMarks
CSS是否直接应用于元素内联?如果是的话,可能会更好。 - Matthew Strawbridge
@QuestionMarks 如果这个方法不行的话,我可以尝试一下。但是jsPDF也是为此而设计的,所以我希望能够解决我的问题。每一个我尝试过的解决方案都有这个样式问题。@Matthew 我尝试了内联样式,但也没有起作用。 - Virge Assault
1个回答

7
我最终通过使用 html2canvasjsPDF 实现了此功能。 隐藏自身在pdf中的按钮,使用html2canvas选项:
<div data-html2canvas-ignore="true">
    <button id="pdf-new"><a href="javascript:demoFromHTML()" class="button" style="color: black;">Generate PDF</a></button>
</div>

而且脚本适用于html页面底部

<script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        var options = {
            background: '#fff' //background is transparent if you don't set it, which turns it black for some reason.
        };
        pdf.addHTML($('#content')[0], options, function () {
                pdf.save('Test.pdf');
        });
    }
</script>

在html2canvas.js中,我进行了更改:

_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
};

为了避免透明背景变成黑色,请采取以下方法:
_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)" || backgroundColor === undefined);
};

希望能对某人有所帮助!

这会保留链接和图片吗? - Anon
1
你救了我的一天,非常感谢。 - Deepti chipdey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接