xref: /docs/en/application-dev/web/web-print.md (revision e41f4b71)
1# Printing Frontend Pages
2
3With the **\<Web>** component, you can print HTML pages through W3C standards-compliant APIs or application APIs. To start off, declare the [ohos.permission.PRINT](../security/AccessToken/declare-permissions.md) permission.
4
5## Initiating a Print Task Through the W3C Standards-compliant API
6The printing process with W3C is as follows: A print adapter is created, the print application is started, the current web page content is rendered, and the PDF file generated after rendering is transferred to the print framework through the file descriptor (FD). Use the **window.print()** method to print the current document or open the print dialog box. This method does not have any parameter; simply call it in JavaScript.
7
8You can use the frontend CSS styles, for example, **@media print**, to control the printed content. Then load the HTML page in the **\<Web>** component.
9
10- Sample code of the **print.html** page:
11
12  ```html
13  <!DOCTYPE html>
14  <html>
15
16  <head>
17      <meta charset="utf-8">
18      <title>printTest</title>
19      <style>
20          @media print {
21              h1 {
22                  display: none;
23              }
24          }
25      </style>
26  </head>
27
28  <body>
29      <div>
30          <h1><b>
31                  <center>This is a test page for printing</center>
32              </b>
33              <hr color=#00cc00 width=95%>
34          </h1>
35          <button class="Button Button--outline" onclick="window.print();">Print</button>
36          <p> content content content </p>
37          <div id="printableTable">
38              <table>
39                  <thead>
40                      <tr>
41                          <td>Thing</td>
42                          <td>Chairs</td>
43                      </tr>
44                  </thead>
45                  <tbody>
46                      <tr>
47                          <td>1</td>
48                          <td>blue</td>
49                      </tr>
50                      <tr>
51                          <td>2</td>
52                          <td>green</td>
53                      </tr>
54                  </tbody>
55              </table>
56          </div>
57          <p> content content content </p>
58          <p> content content content </p>
59      </div>
60  </body>
61  ```
62
63- Application code:
64
65  ```ts
66  import { webview } from '@kit.ArkWeb';
67
68  @Entry
69  @Component
70  struct Index {
71    controller: webview.WebviewController = new webview.WebviewController();
72
73    build() {
74      Row() {
75        Column() {
76          Web({ src: $rawfile("print.html"), controller: this.controller })
77            .javaScriptAccess(true)
78        }
79        .width('100%')
80      }
81      .height('100%')
82    }
83  }
84  ```
85
86## Initiating a Print Task Through the Application API
87On the application side, call [createWebPrintDocumentAdapter](../reference/apis-arkweb/js-apis-webview.md#createwebprintdocumentadapter) to create a print adapter and pass the adapter to the **print** API to initiate printing.
88
89```ts
90// xxx.ets
91import { webview } from '@kit.ArkWeb';
92import { BusinessError } from '@kit.BasicServicesKit';
93import { print } from '@kit.BasicServicesKit'
94
95@Entry
96@Component
97struct WebComponent {
98  controller: webview.WebviewController = new webview.WebviewController();
99
100  build() {
101    Column() {
102      Button('createWebPrintDocumentAdapter')
103        .onClick(() => {
104          try {
105            let webPrintDocadapter = this.controller.createWebPrintDocumentAdapter('example.pdf');
106            print.print('example_jobid', webPrintDocadapter, null, getContext());
107          } catch (error) {
108            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
109          }
110        })
111      Web({ src: 'www.example.com', controller: this.controller })
112    }
113  }
114}
115```
116