我想把我的一些div转换成PDF,我try 了jsPDF库,但没有成功.看来我不明白我需要导入什么才能让图书馆正常工作.我已经看过这些例子了,但我还是搞不懂.我试过以下方法:

<script type="text/javascript" src="js/jspdf.min.js"></script>

在jQuery和:

$("#html2pdf").on('click', function(){
    var doc = new jsPDF();
    doc.fromHTML($('body').get(0), 15, 15, {
        'width': 170
    });
    console.log(doc);
});

用于测试目的,但我收到:

"Cannot read property '#smdadminbar' of undefined"

其中#smdadminbar是主体的第一个div.

推荐答案

您可以使用html中的pdf,如下所示:

步骤1:将以下脚本添加到标头

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>

or download locally

步骤2:添加HTML脚本以执行jsPDF代码

对此进行自定义以传递标识符,或者仅将#CONTENT更改为您需要的标识符.

 <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
            '#bypassme': 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>

第三步:添加你的身体内容

<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
    <h1>  
        We support special element handlers. Register them with jQuery-style.
    </h1>
</div>

参考原始教程

看到小提琴了吗

Html相关问答推荐

Chrome中是否有一个本地显示密码功能,用于`input type = password/`?""<"">

输入宽度在表格显示上不起作用FLEX溢出隐藏Firefox

如何阻止Chromecast图标出现在HTML5视频

HTML+CSS+JS动态选中文本高亮 colored颜色 变化

如何保持嵌套进度条的背景色?

Angular -表和源映射中未显示的数据错误

Style=背景图像URL引用变成&;Quot;

可以';t使我的导航栏在HTML/CSS中正确对齐

柔性盒内的物品;t覆盖整个宽度,即使设置为100%

如何将图像放在其内部按钮下方

为 HTML5 文本字段设置最后六位正则表达式模式

使用修饰键微调 HTML 输入范围中的值

在 CSS 中的 div 中获取文本以环绕其他文本?

如何将背景与之前的内容正确对齐

提交前的验证表单未显示任何验证消息?

圆形边框显示在该部分后面.怎么修?

打开 Bootstrap 模式时无法获得模糊的背景

让 Bootstrap 5 模式和背景占据父容器的宽度和高度,而不是全屏

两个按钮范围滑块的 CSS

如何使用 :hover zoom 重叠图像?