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

NodeJS实现:将含HTML的JSON响应转换为PDF文件

Got it, let's walk through how to pull that API data and turn the name/description fields into a PDF. Here's a step-by-step breakdown with code examples you can adapt:

实现步骤:从API获取数据并生成PDF

1. 发送GET请求获取JSON数据

First, we'll fetch the data from your local endpoint. Using Python's requests library makes this straightforward:

import requests

# 发送GET请求并解析JSON
response = requests.get('http://localhost:4000/features')
features_data = response.json()

This will give you the array of feature objects you shared in your example.

2. 处理带HTML标签的description字段

The description field has HTML markup (like <p> or <strong>). You have two options here: extract plain text, or keep the formatting for the PDF.

提取纯文本

Use BeautifulSoup to strip out HTML tags and get clean text:

from bs4 import BeautifulSoup

for feature in features_data:
    # 保留name字段
    feature_name = feature['name']
    # 解析HTML并提取纯文本
    soup = BeautifulSoup(feature['description'], 'html.parser')
    clean_description = soup.get_text(strip=True)

保留HTML格式

If you want to keep the bold text and paragraph structure, you can reuse the raw HTML directly in your PDF generation (we'll cover this in the next step).

3. 生成PDF文档

Here are two reliable methods to create the PDF, depending on whether you want plain text or styled content:

方法1:用ReportLab生成纯文本PDF

Great for simple, custom layouts:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from bs4 import BeautifulSoup

# 初始化PDF画布
pdf = canvas.Canvas('features.pdf', pagesize=letter)
page_width, page_height = letter
y_pos = page_height - 50  # 起始位置

# 设置字体
pdf.setFont("Helvetica-Bold", 14)

for idx, feature in enumerate(features_data):
    # 写入feature名称
    pdf.drawString(50, y_pos, feature['name'])
    y_pos -= 20
    
    # 写入纯文本描述
    pdf.setFont("Helvetica", 12)
    soup = BeautifulSoup(feature['description'], 'html.parser')
    clean_desc = soup.get_text(strip=True)
    
    # 处理长文本换行
    text_box = pdf.beginText(50, y_pos)
    text_box.textLines(clean_desc)
    pdf.drawText(text_box)
    
    # 预留间距,自动换页
    y_pos -= 100
    if y_pos < 50:
        pdf.showPage()
        pdf.setFont("Helvetica-Bold", 14)
        y_pos = page_height - 50

# 保存PDF
pdf.save()

方法2:用WeasyPrint生成带样式的PDF

Perfect if you want to retain the original HTML formatting (like bold text):

import weasyprint

# 构建完整的HTML内容
html_template = "<html><body style='margin: 2rem;'>"
for feature in features_data:
    html_template += f"<h2 style='color: #2c3e50;'>{feature['name']}</h2>"
    html_template += feature['description']  # 直接插入原始HTML
html_template += "</body></html>"

# 生成带样式的PDF
weasyprint.HTML(string=html_template).write_pdf('features_styled.pdf')

额外注意事项

  • Install required dependencies first: pip install requests beautifulsoup4 reportlab weasyprint
  • WeasyPrint may need system-level dependencies (like libpango) depending on your OS—check the library docs for setup details
  • Adjust fonts, spacing, or page sizes in the code to match your desired output

内容的提问来源于stack exchange,提问作者Lars Suffys

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:18:20