We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesApplication FrameworkArkWebProcessing Web Page ContentPrinting Frontend Pages

Printing Frontend Pages

The Web component can print HTML pages in two ways: through the W3C standard protocol API and through the app-side API.

Before using the print capability, declare related permissions in the module.json5 file. For details, see Declaring Permissions in the Configuration File.

Collapse
Word wrap
Dark theme
Copy code
  1. "requestPermissions":[
  2. {
  3. "name" : "ohos.permission.PRINT"
  4. }
  5. ]

Initiating a Print Task Through the W3C Standards-compliant API

A print adapter is created to start the print app and render the current web page content. The PDF file information generated after rendering is passed to the print framework through a file descriptor (fd). The W3C standard protocol API window.print() is used to print the current page or pop up a print dialog box. This method takes no parameters and only needs to be called in JavaScript.

You can use frontend CSS styles to control whether to print, for example, @media print. Then load the HTML page in the Web component.

  • Sample code of the print.html page:

    Example 1:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>printTest</title>
    6. <style>
    7. @media print {
    8. h1 {
    9. display: none;
    10. }
    11. }
    12. </style>
    13. </head>
    14. <body>
    15. <div>
    16. <h1><b>
    17. <p style="text-align: center;">This is a test page for printing</p>
    18. </b>
    19. <hr color="#00cc00" width="95%">
    20. </h1>
    21. <button class="Button Button--outline" onclick="window.print();">Print</button>
    22. <p> content content content </p>
    23. <div id="printableTable">
    24. <table>
    25. <thead>
    26. <tr>
    27. <td>Thing</td>
    28. <td>Chairs</td>
    29. </tr>
    30. </thead>
    31. <tbody>
    32. <tr>
    33. <td>1</td>
    34. <td>blue</td>
    35. </tr>
    36. <tr>
    37. <td>2</td>
    38. <td>green</td>
    39. </tr>
    40. </tbody>
    41. </table>
    42. </div>
    43. <p> content content content </p>
    44. <p> content content content </p>
    45. </div>
    46. </body>

    Example 2 (nesting a page in an iframe):

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Print Nested Page in an iframe</title>
    7. </head>
    8. <body>
    9. <button id="printIframe">Print nested page in an iframe</button>
    10. <iframe id="contentIframe" hidden></iframe>
    11. <script>
    12. document.getElementById("printIframe").addEventListener("click", () => {
    13. var ctIframe = document.getElementById("contentIframe");
    14. if(!ctIframe.contentWindow || !ctIframe.contentWindow.document) {
    15. console.error("Failed to initialize the iframe page");
    16. return;
    17. }
    18. var ctIframeDoc = ctIframe.contentWindow.document;
    19. ctIframeDoc.write("Nested page");
    20. ctIframeDoc.close();
    21. ctIframe.contentWindow.print();
    22. });
    23. </script>
    24. </body>
    25. </html>
  • Application code:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { webview } from '@kit.ArkWeb';
    2. @Entry
    3. @Component
    4. struct Index {
    5. controller: webview.WebviewController = new webview.WebviewController();
    6. build() {
    7. Row() {
    8. Column() {
    9. Web({ src: $rawfile('print.html'), controller: this.controller })
    10. .javaScriptAccess(true)
    11. }
    12. .width('100%')
    13. }
    14. .height('100%')
    15. }
    16. }

Initiating a Print Task Through the Application API

On the application side, call createWebPrintDocumentAdapter to create a print adapter and pass the adapter to the print API to initiate printing.

Collapse
Word wrap
Dark theme
Copy code
  1. import { webview } from '@kit.ArkWeb';
  2. import { BusinessError, print } from '@kit.BasicServicesKit';
  3. @Entry
  4. @Component
  5. struct WebComponent {
  6. controller: webview.WebviewController = new webview.WebviewController();
  7. build() {
  8. Column() {
  9. Button('createWebPrintDocumentAdapter')
  10. .onClick(() => {
  11. try {
  12. let webPrintDocAdapter = this.controller.createWebPrintDocumentAdapter('example.pdf');
  13. print.print('example_job_id', webPrintDocAdapter, null, this.getUIContext().getHostContext());
  14. } catch (error) {
  15. console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
  16. }
  17. })
  18. Web({ src: 'www.example.com', controller: this.controller });
  19. }
  20. }
  21. }
Search in Guides
Enter a keyword.