首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用javascript打印barCode或导出pdf格式的barCode

如何使用javascript打印barCode或导出pdf格式的barCode
EN

Stack Overflow用户
提问于 2016-12-04 10:02:13
回答 1查看 6.9K关注 0票数 1

我在我的application.There中生成了application.There中的一些问题,比如打印barCode和导出pdf file.In --这是barCode没有显示在打印或pdf文件中的情况,我使用angularjs js指令生成barCod。

这是我的JavaScript

代码语言: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

代码语言:javascript
复制
                    <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-04 15:42:49

您需要在新窗口中包含CSS和媒体类型为all,否则它将在打印中被忽略。此外,浏览器没有打印条形码时也会出现问题,因为默认情况下,它们会禁用打印背景图像。您可以更改Chrome的CSS,这样它就可以覆盖这个。但是,在IE中,用户需要从工具->Print-> page setup中更改他们的页面设置选项。

将$timeout服务添加到控制器中,并按如下方式更改_printBarCode函数:

代码语言:javascript
复制
.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单击需要引用页面上元素的id:

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40957436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档