lodop Lodop打印

Lodop 打印的基础实现,商用需要购买KEY。(同时感谢 lodop 为 ng-alain 免费提供正版KEY)

运行示例的前提条件必须安装Lodop

  1. import { LodopModule } from '@delon/abc/lodop';

代码演示

lodop Lodop打印 - 图1

基础

获取打印服务器(包括:远程)信息。

  1. import { Component } from '@angular/core';
  2. import { NzMessageService } from 'ng-zorro-antd/message';
  3. import { LodopService, Lodop } from '@delon/abc/lodop';
  4. @Component({
  5. selector: 'components-lodop-print',
  6. template: `
  7. <nz-alert *ngIf="error" [nzType]="'warning'" [nzMessage]="message">
  8. <ng-template #message>
  9. 请先下载<a href="http://c-lodop.com/download.html" target="_blank">Lodop插件</a>, 安装后点击<a (click)="reload()">重试</a>。
  10. </ng-template>
  11. </nz-alert>
  12. <form *ngIf="!error" nz-form>
  13. <nz-form-item nz-row>
  14. <nz-form-label nz-col [nzSm]="6">打印服务器</nz-form-label>
  15. <nz-form-control nz-col [nzSm]="18">
  16. <nz-input-group>
  17. <div nz-col [nzSpan]="16">
  18. <input nz-input nzPlaceHolder="https://localhost:8443/CLodopfuncs.js" [(ngModel)]="cog.url" name="url" />
  19. </div>
  20. <div nz-col [nzSpan]="8">
  21. <button nz-button (click)="reload(null)">重新加载打印机</button>
  22. </div>
  23. </nz-input-group>
  24. </nz-form-control>
  25. </nz-form-item>
  26. <nz-form-item nz-row>
  27. <nz-form-label nz-col [nzSm]="6">打印机</nz-form-label>
  28. <nz-form-control nz-col [nzSm]="18">
  29. <nz-select
  30. style="width:90%;"
  31. nzPlaceHolder="请选择打印机"
  32. nzShowSearch
  33. nzAllowClear
  34. [(ngModel)]="cog.printer"
  35. name="printer"
  36. (ngModelChange)="changePinter($event)"
  37. >
  38. <nz-option *ngFor="let name of pinters" [nzLabel]="name" [nzValue]="name"> </nz-option>
  39. </nz-select>
  40. </nz-form-control>
  41. </nz-form-item>
  42. <nz-form-item nz-row>
  43. <nz-form-label nz-col [nzSm]="6">纸张类型</nz-form-label>
  44. <nz-form-control nz-col [nzSm]="18">
  45. <nz-select style="width:90%;" nzPlaceHolder="请选择纸张类型" nzShowSearch nzAllowClear [(ngModel)]="cog.paper" name="paper">
  46. <nz-option *ngFor="let name of papers" [nzLabel]="name" [nzValue]="name"> </nz-option>
  47. </nz-select>
  48. </nz-form-control>
  49. </nz-form-item>
  50. <nz-form-item nz-row>
  51. <nz-form-label nz-col [nzSm]="6">打印内容</nz-form-label>
  52. <nz-form-control nz-col [nzSm]="18" nzExtra="仅限HTML,更多类型支持请参考官网">
  53. <textarea nz-input [(ngModel)]="cog.html" name="html" nzAutosize></textarea>
  54. </nz-form-control>
  55. </nz-form-item>
  56. <nz-form-item nz-row>
  57. <nz-form-control nz-col [nzSm]="18" [nzOffset]="6">
  58. <button nz-button (click)="print(true)" [nzLoading]="printing">打印预览</button>
  59. <button nz-button (click)="print()" [nzLoading]="printing">直接打印</button>
  60. </nz-form-control>
  61. </nz-form-item>
  62. </form>
  63. `,
  64. })
  65. export class ComponentsLodopPrintComponent {
  66. cog: any = {
  67. url: 'https://localhost:8443/CLodopfuncs.js',
  68. printer: '',
  69. paper: '',
  70. html: `
  71. <h1>Title</h1>
  72. <p>这~!@#¥%……&*()——sdilfjnvn</p>
  73. <p>这~!@#¥%……&*()——sdilfjnvn</p>
  74. <p>这~!@#¥%……&*()——sdilfjnvn</p>
  75. <p>这~!@#¥%……&*()——sdilfjnvn</p>
  76. <p>这~!@#¥%……&*()——sdilfjnvn</p>
  77. `,
  78. };
  79. error = false;
  80. lodop: Lodop | null = null;
  81. pinters: any[] = [];
  82. papers: string[] = [];
  83. constructor(public lodopSrv: LodopService, private msg: NzMessageService) {
  84. this.lodopSrv.lodop.subscribe(({ lodop, ok }) => {
  85. if (!ok) {
  86. this.error = true;
  87. return;
  88. }
  89. this.error = false;
  90. this.msg.success(`打印机加载成功`);
  91. this.lodop = lodop as Lodop;
  92. this.pinters = this.lodopSrv.printer;
  93. });
  94. }
  95. reload(options: any = { url: 'https://localhost:8443/CLodopfuncs.js' }) {
  96. this.pinters = [];
  97. this.papers = [];
  98. this.cog.printer = '';
  99. this.cog.paper = '';
  100. this.lodopSrv.cog = Object.assign({}, this.cog, options);
  101. this.error = false;
  102. if (options === null) this.lodopSrv.reset();
  103. }
  104. changePinter(name: string) {
  105. this.papers = this.lodop!.GET_PAGESIZES_LIST(name, '\n').split('\n');
  106. }
  107. printing = false;
  108. print(isPrivew = false) {
  109. const LODOP = this.lodop!;
  110. LODOP.PRINT_INITA(10, 20, 810, 610, '测试C-Lodop远程打印四步骤');
  111. LODOP.SET_PRINTER_INDEXA(this.cog.printer);
  112. LODOP.SET_PRINT_PAGESIZE(0, 0, 0, this.cog.paper);
  113. LODOP.ADD_PRINT_TEXT(1, 1, 300, 200, '下面输出的是本页源代码及其展现效果:');
  114. LODOP.ADD_PRINT_TEXT(20, 10, '90%', '95%', this.cog.html);
  115. LODOP.SET_PRINT_STYLEA(0, 'ItemType', 4);
  116. LODOP.NewPageA();
  117. LODOP.ADD_PRINT_HTM(20, 10, '90%', '95%', this.cog.html);
  118. if (isPrivew) LODOP.PREVIEW();
  119. else LODOP.PRINT();
  120. }
  121. }

lodop Lodop打印 - 图2

打印设计

使用打印设计对字体、字号、对象排版等,并利用 attachCode 方法自动附加设计生成的代码至 Lodop 对象,且支持动态参数。调用 print 方法来打印数据并通过监听 events 判断进行下一个打印,从而实现无限制批量套打。

  1. import { Component } from '@angular/core';
  2. import { NzMessageService } from 'ng-zorro-antd/message';
  3. import { LodopService, Lodop } from '@delon/abc/lodop';
  4. @Component({
  5. selector: 'components-lodop-design',
  6. template: `
  7. <nz-alert *ngIf="error" [nzType]="'warning'" [nzMessage]="message">
  8. <ng-template #message>
  9. 请先下载<a href="http://c-lodop.com/download.html" target="_blank">Lodop插件</a>。
  10. </ng-template>
  11. </nz-alert>
  12. <form *ngIf="lodop && !error" nz-form>
  13. <nz-form-item nz-row>
  14. <nz-form-label nz-col [nzSm]="6">标题</nz-form-label>
  15. <nz-form-control nz-col [nzSm]="18">
  16. <input nz-input [(ngModel)]="context['标题']" name="标题">
  17. </nz-form-control>
  18. </nz-form-item>
  19. <nz-form-item nz-row>
  20. <nz-form-label nz-col [nzSm]="6">费用</nz-form-label>
  21. <nz-form-control nz-col [nzSm]="18">
  22. <input nz-input [(ngModel)]="context['费用']" name="费用">
  23. </nz-form-control>
  24. </nz-form-item>
  25. <nz-form-item nz-row>
  26. <nz-form-label nz-col [nzSm]="6">打印内容</nz-form-label>
  27. <nz-form-control nz-col [nzSm]="18">
  28. <textarea [(ngModel)]="code" name="code" nz-input [nzAutosize]="{ minRows: 2, maxRows: 6 }"></textarea>
  29. </nz-form-control>
  30. </nz-form-item>
  31. <nz-form-item nz-row>
  32. <nz-form-control nz-col [nzSm]="18" [nzOffset]="6">
  33. <button nz-button (click)="design()" [nzLoading]="doing">打印设计</button>
  34. <button nz-button (click)="setup()">打印维护</button>
  35. <button nz-button (click)="print()">打印预览</button>
  36. <button nz-button (click)="printBatch()" [nzLoading]="doing">批量打印3张</button>
  37. </nz-form-control>
  38. </nz-form-item>
  39. </form>
  40. `
  41. })
  42. export class ComponentsLodopDesignComponent {
  43. doing = false;
  44. error = false;
  45. lodop: Lodop | null = null;
  46. context: any = {
  47. 标题: '自定义标题',
  48. 费用: '100.00 元'
  49. };
  50. code = `LODOP.PRINT_INITA(10,10,762,533,"测试套打");
  51. LODOP.ADD_PRINT_TEXT(38,78,408,30,"{{标题}}");
  52. LODOP.SET_PRINT_STYLEA(0,"FontSize",15);
  53. LODOP.SET_PRINT_STYLEA(0,"FontColor","#800000");
  54. LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
  55. LODOP.ADD_PRINT_TEXT(259,579,100,23,"{{费用}}");
  56. LODOP.ADD_PRINT_TEXT(260,520,58,24,"合计:");`;
  57. constructor(public lodopSrv: LodopService, private msg: NzMessageService) {
  58. this.lodopSrv.lodop.subscribe(({ lodop, ok }) => {
  59. if (!ok) {
  60. this.error = true;
  61. return;
  62. }
  63. this.error = false;
  64. this.lodop = lodop as Lodop;
  65. });
  66. }
  67. design() {
  68. this.lodopSrv.attachCode(this.code);
  69. this.doing = true;
  70. this.lodopSrv.design().then((code) => {
  71. this.code = code;
  72. this.doing = false;
  73. });
  74. }
  75. setup() {
  76. this.lodopSrv.attachCode(this.code);
  77. this.lodop!.PRINT_SETUP();
  78. }
  79. print() {
  80. this.lodopSrv.attachCode(this.code, this.context);
  81. this.lodop!.PREVIEW();
  82. }
  83. printBatch() {
  84. this.doing = true;
  85. const data = new Array(3).fill({}).map((_item, index) => Object.assign({ index: index + 1 }, this.context));
  86. this.lodopSrv.print(this.code + `
  87. LODOP.ADD_PRINT_TEXT(10,10,100,100,"第{{index}}张");
  88. `, data);
  89. const batch$ = this.lodopSrv.events.subscribe((res) => {
  90. console.log('finish', res);
  91. if (res.item.index === data.length - 1) {
  92. this.msg.success(`全部打印完成`);
  93. this.doing = false;
  94. batch$.unsubscribe();
  95. }
  96. });
  97. }
  98. }

API

LodopService

成员说明类型默认值
cog获取或设置配置项AlainLodopConfig-
events打印过程通知Observable<LodopPrintResult>-
lodop获取 Lodop 对象Observable<LodopResult>-

Lodop加载成功后辅助API

成员说明类型默认值
printer获取打印机列表string[]-
attachCode()附加代码至 lodop 对象上void-
design()运行打印设计手动关闭后返回代码Promise<string>-
print()立即打印,一般用于批量套打void-

LodopPrintResult

成员说明类型默认值
ok是否打印成功boolean-
error错误信息string-
code代码string-
item动态参数上下文对象any-
parser代码解析表达式RegExp-

LodopResult

成员说明类型默认值
ok表示是否加载成功boolean-
status状态码string-
lodopLodop 对象Lodop-
error错误明细any-

AlainLodopConfig

成员说明类型默认值全局配置
companyName注册单位名称string-
license主注册号string-
licenseA附加注册号Astring-
licenseB附加注册号Bstring-
urlLodop 远程脚本URL地址,注意务必使用 name 属性指定变量值string//localhost:8443/Lodopfuncs.js
nameLodop 变量名stringLODOP
checkMaxCount检查次数,默认 100,当检查超过时视为异常,这是因为 Lodop 需要连接 WebSocketnumber100