Add watermarks to PDFs with Function Compute

更新时间:
复制 MD 格式

This topic describes how to use a Function Compute node in DataWorks to invoke Function Compute and periodically add watermarks to incremental PDF files in OSS.

Background

DataWorks uses a Function Compute node to invoke Function Compute. This allows you to run custom logic as part of a DataWorks workflow.

Prerequisites

  • DataWorks is activated. For more information, see Activate DataWorks.

  • Function Compute is activated. For more information, see Quickly create a function.

  • OSS is activated. For more information, see Activate OSS. Create a bucket and upload the PDF files that you want to watermark. For this example, create a directory named 2023-08-15 in the bucket bucket-test222, and upload example.pdf to this directory.

Limitations

  • Feature limitations

    DataWorks supports only event functions, not HTTP functions. Therefore, to periodically schedule an event-processing task in DataWorks, you must create an event function. For more information about function types, see Function types.

  • Region limitations

    Function Compute nodes are available only in workspaces that reside in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Shenzhen), China (Chengdu), China (Hong Kong), Singapore, Malaysia (Kuala Lumpur), Indonesia (Jakarta), Germany (Frankfurt), UK (London), US (Silicon Valley), and US (Virginia).

Step 1: Create a Function Compute application

  1. Log in to the Function Compute console. In the navigation pane on the left, click Application.

  2. On the Applications page, click Create Application, and select Use a Template to Create an Application. In the search box, search for "add watermark to PDF files". In the template list, find the pdf-watermark template, hover the pointer over its card, and then click Create Now.

    image

    Note

    The source code for the pdf-watermark application is available on GitHub. This application adds a specified watermark to a PDF file in OSS and saves the watermarked file to a new file in the same OSS location.

  3. Configure the parameters for Create Application.

    Parameter

    Description

    Deployment type

    Select Directly Deploy.

    Application Name

    A compliant name is automatically generated. You can change it based on your business requirements.

    Role name

    By default, AliyunFCServerlessDevsRole is selected. You can configure the policies for this role as needed.

    • When deploying an application from Serverless Application Center, you may need to access other cloud services (such as VPC, NAS, and SLS) to deploy or update resources. To do this, you must grant Function Compute the necessary permissions by associating a RAM service role with the application and setting Function Compute as a trusted service. Serverless Application Center then uses the AssumeRole operation to access these services on your behalf.

    • To simplify the authorization process, Serverless Application Center provides a default system role named AliyunFCServerlessDevsRole. This role includes permissions to access some of the cloud resources required by Serverless Application Center. You can log in to the RAM console to view the permissions granted to the AliyunFCServerlessDevsRole role.

    Region

    The region where the application is created. When you select an OSS bucket name, you can only choose an OSS bucket in this region.

    Function Name

    A compliant name is automatically generated. You can change it based on your business requirements.

    Time Zone

    The time zone of the current region is automatically selected. You can change it based on your business requirements.

    OSS bucket name

    You can select only a bucket that is in the same region as the application.

    RAM role ARN

    By default, AliyunFcDefaultRole is selected. You can change it as needed.

    To simplify authorization, Function Compute provides a default service role named AliyunFcDefaultRole. This role contains the permissions Function Compute needs to access some cloud resources. For information about how to create and bind the default role AliyunFcDefaultRole, see Step 1: Activate Function Compute.

    Note

    If you are prompted that the selected application requires additional permissions during creation, click Authorized to go.

  4. Click Create and Deploy Default Environment. When Deployed is displayed on the details page, the deployment is complete.

    image.png

  5. On the Applications page, click the application name to go to the application details page.

    image

  6. In the Default Environment section, click the Environment Details tab, and then click the Function name to go to the Function details page.

    image

  7. Click Test, enter the parameters, and test the function.

    图片编辑

    • Event name: Enter an event name.

    • Event content: Enter the content in JSON format. This topic uses the following example:

      Important

      If you copy the following JSON example, you must remove the comments that start with //. Otherwise, the content fails JSON validation.

      // The following configuration adds the watermark text "DataWorks" to the PDF file 2023-08-15/example.pdf by using the 20-point Helvetica font. For the meaning of each parameter, see the comments that follow.
      {
          "pdf_file": "2023-08-15/example.pdf",  // The path of the PDF file in the OSS bucket.
          "mark_text": "DataWorks",    // The watermark text. This parameter is required if you want to add a watermark to a PDF file.
          "pagesize": [595.275590551181, 841.8897637795275], // Optional. The default page size is A4 (21 cm × 29.7 cm), where 1 cm is equal to 28.346456692913385.
          "font": "Helvetica",     // Optional. The default font is Helvetica. For Chinese text, you can use zenhei or microhei.
          "font_size": 20,         // Optional. The default font size is 30.
          "font_color": [0, 0, 0], // The font color in RGB format. The default color is black.
          "rotate": 30,            // Optional. The default rotation angle is 0.
          "opacity": 0.1,          // Optional. The default opacity is 0.1. A value of 1 indicates that the text is opaque.
          "density": [198.4251968503937, 283.46456692913387] // The watermark density. The default value is [141.73228346456693, 141.73228346456693], which corresponds to (7 cm, 10 cm). This means the interval between watermarks is 7 cm on the x-axis and 10 cm on the y-axis.
      }
  • Click Test Function. After the execution succeeds, you can view the watermarked PDF file in the same OSS directory as the source file. In this example, the output file is example-out.pdf.

    image.png

    View the files in OSS:

    image.png

Step 2: Configure a Function Compute node

  1. Log in to the DataWorks console.

  2. In the top navigation bar, switch to the region that you specified in Step 1: Create a Function Compute application.

  3. In the navigation pane on the left, choose Data Development and O&M > Data Studio. Select your workspace to go to the Data Development page.

  4. Click the name of the target Workflow. In the expanded General node, right-click and select Function Compute. In the dialog box that appears, enter a node name and click Determine to create the Function Compute node.

  5. Configure the Function Compute node parameters.

    设置函数计算节点参数

    Parameter

    Description

    Select Function

    Select the function that you created in Step 1. To create a new function, see Manage functions.

    Note

    DataWorks supports only event functions, not HTTP functions. Therefore, to periodically schedule an event-processing task in DataWorks, you must create an event function. For more information about function types, see Function types.

    Select Version or Alias

    Select the service version or alias to use when invoking the function. The default version is LATEST. In this example, select Default Version.

    • Service version

      Function Compute provides a service-level versioning feature, which allows you to deploy one or more versions for your service. When you deploy a version, Function Compute creates a snapshot of the service. The snapshot includes the service configuration and the code and configuration of functions within the service, but does not include triggers. Function Compute automatically associates an incremental version number with the snapshot. For more information, see Deploy a version.

    • Version alias

      Function Compute allows you to create an alias for a service version. An alias points to a specific version. You can use an alias to facilitate deployment, rollback, and canary releases. An alias cannot exist independently of a service or version. When you use an alias to access a service or function, Function Compute resolves the alias to the version it points to. The invoker does not need to know the specific version. For more information, see Create an alias.

    Invocation Method

    Select and synchronizes for this example. For more information, see synchronous invocation and asynchronous invocation.

    • Synchronous invocation: The event directly triggers the function. Function Compute runs the function and waits for a response. When the function call is complete, Function Compute returns the execution result directly.

    • Asynchronous invocation: Function Compute persists the event request and immediately returns a response, instead of waiting for the request to finish.

      • If your function is time-consuming, resource-intensive, or error-prone, use asynchronous invocation to improve responsiveness and handle traffic spikes more reliably.

      • Use an asynchronous invocation for Function Compute tasks that run for more than one hour.

    Variable

    These are the parameters for invoking the function. This example modifies the JSON from the Event content section to add watermarks to new PDF files in OSS each day.

    // The following configuration adds a watermark to the PDF file at ${current_date}/example.pdf.
    {
        "pdf_file": "${current_date}/example.pdf",  // The path of the PDF file in the OSS bucket.
        "mark_text": "DataWorks",    // The watermark text. This parameter is required if you want to add a watermark to a PDF file.
        "pagesize": [595.275590551181, 841.8897637795275], // Optional. The default page size is A4 (21 cm × 29.7 cm), where 1 cm is equal to 28.346456692913385.
        "font": "Helvetica",     // Optional. The default font is Helvetica. For Chinese text, you can use zenhei or microhei.
        "font_size": 20,         // Optional. The default font size is 30.
        "font_color": [0, 0, 0], // The font color in RGB format. The default color is black.
        "rotate": 30,            // Optional. The default rotation angle is 0.
        "opacity": 0.1,          // Optional. The default opacity is 0.1. A value of 1 indicates that the text is opaque.
        "density": [198.4251968503937, 283.46456692913387] // The watermark density. The default value is [141.73228346456693, 141.73228346456693], which corresponds to (7 cm, 10 cm). This means the interval between watermarks is 7 cm on the x-axis and 10 cm on the y-axis.
    }
    Note
    • In the value of pdf_file, which is ${current_date}/example.pdf, ${current_date} represents a variable named current_date.

    • When DataWorks schedules a task, it replaces ${current_date} with the actual value. You can configure this variable in the scheduling parameters. For example, pdf_file is 2023-08-15/example.pdf when the task is run on August 15, 2023, and pdf_file is 2023-08-16/example.pdf when the task is run on August 16, 2023.

    • Your business system only needs to generate PDF files to the corresponding path before the scheduled task runs. This allows for dynamically adding watermarks to new PDF files daily.

    • Before you run the task, you must upload a PDF file that matches the path ${current_date}/example.pdf to OSS. For example, 2023-08-15/example.pdf.

  6. Optional: Debug the Function Compute node. After you configure the node, click the 运行 icon, specify a resource group for the task, and assign a constant to the code variable for debugging. This tests whether the node's code logic is correct. For example, setting current_date=2023-08-15 means Function Compute adds a watermark to the 2023-08-15/example.pdf file in OSS.

  7. Configure the periodic scheduling properties for the node. DataWorks provides scheduling parameters to enable dynamic parameter passing in scheduling scenarios. Open the Scheduling pane on the right and set parameters in the Parameter section. This example adds a parameter named current_date with the value $[yyyy-mm-dd], which represents the current year, month, and day when the task runs. For more information about scheduling parameters, see Supported formats for scheduling parameters. For more information about scheduling properties, see Task scheduling properties overview.

    image.png

Step 3: Commit and deploy the node

Before a Function Compute node can run on schedule, you must commit and deploy it to the production environment.

  1. Save and commit the node.

    Click the 保存 and 提交 icons on the toolbar to save and commit the node. When you commit the node, enter a change description as prompted, and optionally enable code review and smoke testing.

    Note
    • You must configure the Rerun attribute and specify the Parent Nodes in the scheduling properties before you can commit the node.

    • If code review is enabled, the node code submitted by a developer can be deployed only after it is approved by a reviewer. For more information, see Code review.

    • To ensure that the scheduled node runs as expected, we recommend that you perform smoke testing on the task before deployment. For more information, see Smoke testing.

  2. Optional: Deploy the node.

    If you use a workspace in standard mode, click Deploy in the upper-right corner after you commit the node. For more information, see Workspaces in standard mode and Deploy tasks.

Next steps

Related documents