Add watermarks to PDFs with Function Compute
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
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 invoking only event functions, not HTTP functions. Therefore, you must use an event function for tasks that you want to schedule periodically. For more information about function types, see Function types.
-
Region limitations
The Function Compute feature is 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
Log in to the Function Compute console. In the navigation pane on the left, click Application.
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.
NoteThe 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.
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 you deploy an application through Serverless App Center, you may need to access other cloud services. For example, you may need to deploy Function Compute services and function resources, or create or update VPC, NAS, and SLS resources. In these cases, you must grant Function Compute the required access permissions. First, associate a RAM service role with the application or environment, and then set the trusted service to Function Compute. Serverless App Center uses AssumeRole to access your cloud services.
To simplify authorization, Serverless App Center provides a default system role named AliyunFCServerlessDevsRole. This role contains the permissions that Serverless App Center needs to access some cloud resources. You can log in to the RAM role management console to view the permissions of 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.
NoteIf you are prompted that the selected application requires additional permissions during creation, click Authorized to go.
Click Create and Deploy Default Environment. When Deployed is displayed on the details page, the deployment is complete.
On the Applications page, click the application name to go to the application details page.
In the Default Environment section, click the Environment Details tab, and then click the Function name to go to the Function details page.
The function name is in the Function Resources section of the Resource Information area at the bottom of the page, for example,
pdf_add_watermark_t36b.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:
ImportantIf 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.View the files in OSS:
The OSS bucket file list contains the source file
example.pdfand the generated watermarked fileexample_out.pdf.
Step 2: Configure a Function Compute node
Log in to the DataWorks console.
In the top navigation bar, switch to the region that you specified in Step 1: Create a Function Compute application.
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.
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.
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.
NoteDataWorks supports invoking only event functions, not HTTP functions. Therefore, you must use an event function for tasks that you want to schedule periodically. 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 that allows you to publish one or more versions of your service. When you publish a version, Function Compute creates a snapshot of the service, including its configuration, function code, and function configurations. Triggers are not included. Function Compute then automatically assigns a version number to the snapshot for future use. For more information, see Publish a version.
-
Version alias
Function Compute allows you to create an alias for a service version. An alias points to a specific version, which facilitates operations such as publishing, rollbacks, 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 caller 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. After the function is invoked, Function Compute returns the execution result.
-
Asynchronous invocation: Function Compute queues the event request and returns a response immediately, instead of waiting for the request to complete.
-
If a function is time-consuming, resource-intensive, or contains error-prone logic, you can use asynchronous invocation to improve response speed and reliably handle traffic spikes.
-
For Function Compute tasks that run for more than one hour, use asynchronous invocation.
-
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. }NoteIn the value of
pdf_file, which is${current_date}/example.pdf,${current_date}represents a variable namedcurrent_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_fileis2023-08-15/example.pdfwhen the task is run on August 15, 2023, andpdf_fileis2023-08-16/example.pdfwhen 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.pdfto OSS. For example,2023-08-15/example.pdf.
-
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-15means Function Compute adds a watermark to the2023-08-15/example.pdffile in OSS.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_datewith 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.
Step 3: Commit and deploy the node
To run a Function Compute node on an automatic schedule, you must first commit and deploy it to the production environment.
-
Save and commit the node.
Click the
and
icons in the toolbar to save and commit the node. In the commit dialog box, enter a change description and, if required, select the options for code review and smoke testing.Note-
You must configure the Rerun attribute property and the Parent Nodes in the scheduling properties before you can commit the node.
-
If code review is enabled, the code of a submitted node must be approved by a reviewer before the node can be deployed. 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 you deploy it. For more information, see Smoke testing.
-
-
Optional. Deploy the node.
If you are using a workspace in standard mode, you must click Deploy in the upper-right corner to deploy the node after you commit it. For more information, see Workspaces in standard mode and Deploy tasks.
Next steps
-
After a task is committed and deployed, it is scheduled by Operation Center. You can then manage and monitor the task in the DataWorks Operation Center. For more information, see Operation Center.
-
After you master the basic steps to create and use a Function Compute node, you can explore best practices to gain a deeper understanding of the node. For more information, see Dynamically add watermarks to PDFs by using a Function Compute node in DataWorks.