javascript - 如何使用 javascript 打印条码或以 pdf 格式导出条码

标签 javascript angularjs pdf-generation

我在我的应用程序中生成条码。打印条码和导出 pdf 文件时存在一些问题。在这种情况下,条码未显示在打印或 pdf 文件中。我使用 angularjs js 通过指令生成条码。

这是我的 JavaScript

    $scope._printBarCode = function (printSectionId) {

    var innerContents = document.getElementById(printSectionId).innerHTML;
    var popupWinindow = window.open();//'', '_blank');
    popupWinindow.document.open();
    popupWinindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="Content/assets/css/barCode.css" /></head><body onload="window.print()">' + innerContents + '</html>');
    popupWinindow.document.close();

}

这是我的html

                    <input type="button" value="Prnt" ng-click="_printBarCode('barCodeId')" class="btn btn-primary" />
                            <div class="barcodeplace">
                                <div class="col-sm-12">
                                    <div barcode-generator="{{_barCodeGeneraterId}}" style="height:20px;">
                                </div>
                            </div>
                            </div>

最佳答案

您需要在新窗口中包含媒体类型为 all 的 CSS,否则它会在打印中被忽略。此外,您还会遇到浏览器不打印条形码的问题,因为默认情况下它们禁用打印背景图像。您可以更改 Chrome 的 CSS,以便它覆盖它。但是,在 IE 中,用户需要从工具->打印->页面设置更改他们的页面设置选项。

将 $timeout 服务添加到您的 Controller 并更改 _printBarCode 函数如下:

.controller('MyCtrl', function($scope,$timeout) {
$scope._printBarCode = function(printSectionId) {

    var innerContents = document.getElementById(printSectionId).innerHTML;
    var popupWindow = window.open('', 'Print');

    popupWindow.document.write('<!DOCTYPE html><html><head><style type="text/css">@media print { body { -webkit-print-color-adjust: exact; } }</style><link rel="stylesheet" type="text/css" href="barcode.css" media="all" /></head><body> ' + innerContents + '</body></html>');
    $timeout(function() {
        popupWindow.focus();
        popupWindow.print();
        popupWindow.close();
    });
};

然后在您的 HTML 中,您的 ng-click 需要引用页面上元素的 ID:

<input type="button" value="Prnt" ng-click="_printBarCode('barCodeId')" class="btn btn-primary" />
<div id="barCodeId" class="barcodeplace">
    <div class="col-sm-12">
        <div barcode-generator="{{_barCodeGeneraterId}}" style="height:20px;">
        </div>
    </div>
</div>

关于javascript - 如何使用 javascript 打印条码或以 pdf 格式导出条码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957436/

相关文章:

javascript - 网络音频 API 24db 过滤器

javascript - 更新 Angular d3 文本 onclick

angularjs - 何时不在 AngularJS ng-repeat 中使用 'track by $index'?

php - 使用 dompdf 从数据库渲染数据时出错

javascript - Angular2 单击动态生成的元素

php - 检查数据库中复选框的值

javascript - 试图让我的输出显示在正文部分,格式包括页眉、正文(带背景)和页脚

javascript - $location.path 使用 Angularjs 单击按钮时未加载 html 文件

ruby-on-rails - Prawn pdf嵌套表-找不到例子

matlab - 在 Matlab 中打印元音符号