You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生成DataMatrix码后使用html2pdf导出PDF出现空白页的问题求助

生成DataMatrix码后使用html2pdf导出PDF出现空白页的问题求助

大家好,麻烦帮忙看看我的问题:

我用下面这套代码可以在页面上生成DataMatrix码,但是调用html2pdf()之后,打开的PDF却是空白的。我有一套类似的代码可以正常导出标准2D条码,完全没问题,实在搞不懂哪里出了问题,有没有人能指点一下?

我的完整代码如下:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<h2>DM Code Output Test</h2>
</br>
</br>

    <table width="100%" border="1">
        <tr>
            <td align="left" width="2%" rowspan="2">
                &nbsp;
            </td>
            <td align="left"width="20%">
                <font color="#17365D">Input Value: </font><input type="text" name="ip_source" id="ip_source" size="50"/>
            </td>
            <td align="left">
                      
                <div id="div_barcode" name="div_barcode" class="col-lg-8" align="center">
                    <svg style="display:none" name="barcode" id="barcode">
                    </svg>
                </div>
            </td>
        </tr>
        <tr>
            <td align="right">
                <button name="btn_dmcode" id="btn_dmcode">Press Me</button>
            </td>
                 <td align="left">
                      &nbsp;
            </td>
        </tr>
    </table>

<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.6/dist/JsBarcode.all.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode/1.4.4/qrcode.js" integrity="sha512-oxrVyBhqnzQ0BzuM0A/6dEIk0alz0p4SpDRaWvtuUoarIc8rnL5lVniHG5Dp21MRFojcQcmKHjaskNXhSaUPPw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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

<script src="https://rawcdn.githack.com/GitHub30/dataMatrix.js/97edc524c04db3d30a3fdf00c1fd0988cfa82888/dist/dataMatrix.js"></script>
                
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.3/html2pdf.bundle.min.js"></script>

<script>
$(document).ready(function(){
    const cp = "\u00a0";

    $("#btn_dmcode").click(function(){
        var source = $("#ip_source").val();
        var barcode = document.getElementById("barcode");

        dataMatrix(source)
            .then(barcode => document.body.appendChild(barcode))

        // Choose the element id which you want to export.
        var element = document.getElementById('div_barcode');
        element.style.width = '100px';
        element.style.height = '100px';
        var opt = {
            margin:      0.5,
            filename:    'mydmcode.pdf',
            image:       { type: 'png', quality: 1 },
            html2canvas: { scale: 1 },          
            jsPDF:       { unit: 'mm', format: [50,50], orientation: 'landscape', precision: '12' }         
         };

        // choose the element and pass it to html2pdf() function and call the save() on it to save as pdf.
        html2pdf().set(opt).from(element).save();
        
    });

});
</script>

这是我能正常工作的2D条码JavaScript代码:

var stemassno = document.getElementById("stemassno").textContent;
        var barcode = document.getElementById("barcode");

        JsBarcode(barcode, stemassno, {
            height: 20,
            width: 2
        });

        barcode.style.display = "inline";
        
        // Choose the element id which you want to export.
        var element = document.getElementById('div_barcode');
        element.style.width = '100px';
        element.style.height = '50px';
        var opt = {
            margin:      0.5,
            filename:    'myfile.pdf',
            image:       { type: 'jpeg', quality: 1 },
            html2canvas: { scale: 1 },          
            jsPDF:       { unit: 'mm', format: [60,22], orientation: 'landscape', precision: '12' }         
         };

        // choose the element and pass it to html2pdf() function and call the save() on it to save as pdf.
        html2pdf().set(opt).from(element).save();
        
    });

备注:内容来源于stack exchange,提问作者Gary Croxford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 09:38:10