You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过RESTful服务程序化生成NetSuite采购订单PDF(禁用RESTlets)

程序化生成NetSuite采购订单PDF(RESTful方式,无需RESTlets)

方案1:使用NetSuite REST API的printtask端点(优先推荐)

NetSuite 2020.2及以上版本支持通过REST API创建异步打印任务,生成采购订单PDF,步骤如下:

1. 创建打印任务

通过/record/v1/printtask端点提交请求,指定采购订单ID、打印模板ID和输出类型:

import requests
import json

# 基础配置
base_url = "https://<你的账户ID>.suitetalk.api.netsuite.com/services/rest/record/v1"
headers = {
    "Authorization": "Bearer <你的访问令牌>",
    "Content-Type": "application/json",
    "Accept": "application/json"
}

# 替换为实际采购订单ID和打印模板ID
po_internal_id = "12345"
template_id = "678"  # 默认PO模板或自定义模板的内部ID

# 构建请求体
payload = {
    "recordType": "purchaseorder",
    "recordId": po_internal_id,
    "templateId": template_id,
    "printType": "PDF"
}

# 创建打印任务
response = requests.post(f"{base_url}/printtask", headers=headers, data=json.dumps(payload))
print_task = response.json()
print_task_id = print_task["id"]

2. 轮询任务状态并下载PDF

打印任务为异步执行,需轮询直到状态变为COMPLETE,再通过file端点下载PDF:

import time

# 轮询任务状态
while True:
    status_res = requests.get(f"{base_url}/printtask/{print_task_id}", headers=headers)
    status_data = status_res.json()
    
    if status_data["status"] == "COMPLETE":
        file_id = status_data["file"]["id"]
        break
    elif status_data["status"] == "FAILED":
        raise Exception("打印任务执行失败")
    time.sleep(2)  # 每2秒查询一次

# 下载PDF
pdf_res = requests.get(f"{base_url}/file/{file_id}", headers=headers)
with open(f"PO_{po_internal_id}.pdf", "wb") as f:
    f.write(pdf_res.content)

关键配置说明

  • 打印模板ID可在NetSuite界面获取:进入自定义>表单>打印模板,查看目标模板URL中的id参数
  • 需确保集成角色拥有Print Task记录的创建/查看权限、File记录的查看权限,以及采购订单的查看权限

方案2:SuiteTalk SOAP API(REST API受限时备选)

若你的NetSuite版本不支持REST API的printtask资源,可使用SOAP API的printRecord操作直接生成PDF:

from suds.client import Client

# SOAP WSDL配置
wsdl_url = "https://<你的账户ID>.suitetalk.api.netsuite.com/wsdl/v2024_1_0/netsuite.wsdl"
client = Client(wsdl_url)

# 登录认证
passport = client.factory.create('Passport')
passport.email = "<你的登录邮箱>"
passport.password = "<你的密码>"
passport.account = "<你的账户ID>"
passport.role._internalId = "<集成角色内部ID>"

client.service.login(passport)

# 构建打印请求
print_req = client.factory.create('PrintRequest')
print_req.record.type = "purchaseOrder"
print_req.record.internalId = po_internal_id
print_req.printTemplate.internalId = template_id
print_req.printType = "PDF"

# 执行打印并保存PDF
response = client.service.printRecord(print_req)
with open(f"PO_{po_internal_id}.pdf", "wb") as f:
    f.write(response.content)

client.service.logout()

限制说明

如果你的NetSuite版本早于2020.2,且无法使用RESTlets,目前没有其他纯RESTful的原生方式实现采购订单PDF的程序化生成。

内容的提问来源于stack exchange,提问作者Tony B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 08:02:51