跳到主要内容

14,000+ trusted customers

PDF 工具

每个 PDF 工具。
在线且免费。

直接在浏览器中转换、合并、压缩、拆分、签名和编辑PDF文件。无需安装,无需注册。

40多种可在任何设备上运行的工具。

适用于你已经搭建的地方

直接在工作场所使用 PDF4me。

PDF4me 随时随地满足您的需求:您的浏览器、Microsoft Teams、Google Workspace、您的自动化平台或您自己的代码。

对于开发者

专为开发者设计。

真实、可运行的示例,直接取自我们的开源示例。选择一个端点和一种语言,
复制整个文件并运行它。这就是全部的API。

const fs = require('fs');
const path = require('path');

/**
 * Document to PDF Converter using PDF4Me API
 * Converts various document formats (DOCX, XLSX, etc.) to PDF documents
 * Supports both synchronous and asynchronous processing with retry logic
 */

// API Configuration - PDF4Me service for converting documents to PDF
const API_KEY = "get the API key from https://dev.pdf4me.com/dashboard/#/api-keys/";
const BASE_URL = "https://api.pdf4me.com/";
const API_ENDPOINT = `${BASE_URL}api/v2/ConvertToPdf`;

// File paths configuration
const INPUT_DOC_PATH = "sample.docx";           // Path to input document file
const OUTPUT_PDF_PATH = "Document_to_PDF_output.pdf"; // Output PDF file name

// Retry configuration for async processing
const MAX_RETRIES = 10;
const RETRY_DELAY = 10000; // 10 seconds in milliseconds

/**
 * Main function that orchestrates the document to PDF conversion process
 * Handles file validation, conversion, and result processing
 */
async function convertDocumentToPdf() {
    console.log("Starting Document to PDF Conversion Process...");
    console.log("This converts various document formats (DOCX, XLSX, etc.) to PDF");
    console.log("Supports Word documents, Excel spreadsheets, and other formats");
    console.log("-".repeat(60));

    try {
        // Validate input file exists
        if (!fs.existsSync(INPUT_DOC_PATH)) {
            throw new Error(`Input document file not found: ${INPUT_DOC_PATH}`);
        }

        console.log(`Converting: ${INPUT_DOC_PATH} → ${OUTPUT_PDF_PATH}`);

        // Process the conversion
        const result = await processDocumentToPdfConversion();

        // Handle the result
        await handleConversionResult(result);

    } catch (error) {
        console.error("Conversion failed:", error.message);
        process.exit(1);
    }
}

/**
 * Core conversion logic - handles the API request and response processing
 * Supports both synchronous (200) and asynchronous (202) responses
 */
async function processDocumentToPdfConversion() {
    // Read and encode document file to base64
    console.log("Reading and encoding document file...");
    const docContent = fs.readFileSync(INPUT_DOC_PATH);
    const docBase64 = docContent.toString('base64');
    console.log("Document file successfully encoded to base64");

    // Prepare the conversion payload
    const payload = {
        docContent: docBase64,           // Base64 encoded document content
        docName: "output",               // Name for the output file
        async: true                      // Enable asynchronous processing
    };

    // Set up HTTP headers for authentication and content type
    const headers = {
        "Authorization": `Basic ${API_KEY}`,
        "Content-Type": "application/json"
    };

    console.log("Sending request to PDF4Me API...");

    // Make the initial API request
    const response = await fetch(API_ENDPOINT, {
        method: 'POST',
        headers: headers,
        body: JSON.stringify(payload)
    });

    console.log(`Status code: ${response.status}`);

    // Handle different response scenarios
    if (response.status === 202) {
        // Asynchronous processing - poll for completion
        console.log("Request accepted. PDF4Me is processing asynchronously...");
        
        const locationUrl = response.headers.get('Location');
        if (!locationUrl) {
            throw new Error("No 'Location' header found in the response for polling");
        }

        return await pollForCompletion(locationUrl, headers);

    } else if (response.status === 200) {
        // Synchronous processing - immediate result
        console.log("Document to PDF conversion completed immediately!");
        return await response.arrayBuffer();

    } else {
        // Error response
        const errorText = await response.text();
        throw new Error(`API request failed. Status: ${response.status}, Response: ${errorText}`);
    }
}

/**
 * Handles the conversion result and saves the PDF file
 * Supports both binary PDF data and base64 encoded responses
 */
async function handleConversionResult(result) {
    try {
        // Convert ArrayBuffer to Buffer for file operations
        const buffer = Buffer.from(result);
        
        // Validate that we have a PDF (check for PDF header)
        if (buffer.length > 4 && buffer.toString('ascii', 0, 4) === '%PDF') {
            console.log("Response is a valid PDF file");
            fs.writeFileSync(OUTPUT_PDF_PATH, buffer);
            console.log(`PDF saved successfully to: ${OUTPUT_PDF_PATH}`);
            console.log("Document has been converted to PDF format");
            return;
        }

        // Try to parse as JSON if not a direct PDF
        try {
            const jsonResponse = JSON.parse(buffer.toString());
            console.log("Successfully parsed JSON response");
            
            // Look for PDF data in different possible JSON locations
            let pdfBase64 = null;
            if (jsonResponse.document && jsonResponse.document.docData) {
                pdfBase64 = jsonResponse.document.docData;  // Common location 1
            } else if (jsonResponse.docData) {
                pdfBase64 = jsonResponse.docData;           // Common location 2
            } else if (jsonResponse.data) {
                pdfBase64 = jsonResponse.data;              // Alternative location
            }

            if (pdfBase64) {
                // Decode base64 PDF data and save to file
                const pdfBytes = Buffer.from(pdfBase64, 'base64');
                fs.writeFileSync(OUTPUT_PDF_PATH, pdfBytes);
                console.log(`PDF saved to ${OUTPUT_PDF_PATH}`);
                console.log("Document has been converted to PDF format");
            } else {
                console.log("No PDF data found in the response.");
                console.log("Full response:", JSON.stringify(jsonResponse, null, 2));
            }

        } catch (jsonError) {
            console.log("Failed to parse JSON response, treating as binary data");
            // If JSON parsing fails, try to save as binary anyway
            if (buffer.length > 1000) {
                fs.writeFileSync(OUTPUT_PDF_PATH, buffer);
                console.log(`PDF saved to ${OUTPUT_PDF_PATH} (as binary data)`);
                console.log("Document has been converted to PDF format");
            } else {
                console.log("Warning: Response doesn't appear to be a valid PDF");
                console.log(`First 100 bytes: ${buffer.toString('hex', 0, 100)}`);
            }
        }

    } catch (error) {
        throw new Error(`Error saving PDF: ${error.message}`);
    }
}

/**
 * Polls the API for async completion with retry logic
 * Handles 202 (processing) and 200 (completed) status codes
 */
async function pollForCompletion(locationUrl, headers) {
    console.log(`Polling URL: ${locationUrl}`);

    for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
        console.log(`Waiting for result... (Attempt ${attempt}/${MAX_RETRIES})`);
        
        // Wait before polling (except on first attempt)
        if (attempt > 1) {
            await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
        }

        try {
            const response = await fetch(locationUrl, {
                method: 'GET',
                headers: headers
            });

            if (response.status === 200) {
                // Conversion completed successfully
                console.log("Document to PDF conversion completed successfully!");
                return await response.arrayBuffer();

            } else if (response.status === 202) {
                // Still processing, continue polling
                console.log("Still processing...");
                continue;

            } else {
                // Error occurred during processing
                const errorText = await response.text();
                throw new Error(`Unexpected error during polling: ${response.status}, Response: ${errorText}`);
            }

        } catch (error) {
            if (attempt === MAX_RETRIES) {
                throw new Error(`Polling failed after ${MAX_RETRIES} attempts: ${error.message}`);
            }
            console.log(`Polling attempt ${attempt} failed: ${error.message}`);
        }
    }

    throw new Error(`Timeout: Document to PDF conversion did not complete after ${MAX_RETRIES} retries`);
}

// Main execution - Run the conversion when script is executed directly
if (require.main === module) {
    convertDocumentToPdf().catch(error => {
        console.error("Fatal error:", error.message);
        process.exit(1);
    });
}

module.exports = { convertDocumentToPdf, processDocumentToPdfConversion, handleConversionResult }; 
  • 100API 端点
  • 15API类别
  • 4无代码平台
  • 30+免费在线工具
  • 4扩展
对于开发者

只需单击一下
达到一百万次 API 调用。

人工智能数据提取

将发票和表格转换为清晰的数据。

将 PDF4me 指向发票、收据、银行对账单或合同,即可获得结构化的 JSON 数据,可直接导入您的系统。无需维护此类文档的模板。

  • 现成的解析器,可用于解析发票、收据、银行对账单、工资单、合同等。
  • 返回的字段包括供应商、明细项、总计、日期和税额,格式为 JSON。
  • Power Automate、Make 和 n8n 中的原生操作,以及 REST API 上的相同引擎
探索人工智能数据提取
文档生成

根据模板和数据生成文档。

将 Word 或 HTML 模板与 JSON 合并,生成 PDF 或 Word 文件,可以一次生成一个,也可以批量生成。

  • Word (.docx) 和包含变量、表格和图像的 HTML 模板
  • 从 JSON 数据记录生成单个或批量数据
  • REST API 上的相同生成操作,Power Automate、Make、n8n 和 Zapier
探索文档生成
电子发票

一次通话即可发送 ZUGFeRD 份电子发票。

生成 ZUGFeRD 混合电子发票,一个嵌入发票 XML 的 PDF/A-3 文件,符合德国 B2B 要求和任何 EN 16931 ERP 标准。

  • 混合型 PDF/A-3 + XML、EN 16931 和 Factur-X 对齐
  • 支持基本、舒适和扩展三种合规性,数据格式为 XML、JSON 或 CSV。
  • Power Automate、Make、n8n 和 Zapier 中的原生操作,以及 REST API
探索 ZUGFeRD 发票
解析与分类

构建一个可解析任意文档的解析器。

定义您自己的字段架构并提取您需要的内容,或者让 PDF4me 自动对混合批次进行分类,并按类型路由每个文档。

  • 解析为您自己的 JSON 模式,或按文档类型自动分类
  • 开发者控制面板中内置的自定义分析器模板
  • 根据置信度评分来路由混合文档批次
探索解析与分类
Word 和 Excel 自动化

大规模自动化 Word 和 Excel。

无需安装 Office,即可通过一个 API 将 Word 和 Excel 转换为 PDF,然后再转换回来,并合并、拆分、添加水印和密码保护 Office 文件。

  • Word 变为 PDF,Excel 变为 PDF,PDF 变回 Word 或 Excel
  • 合并、拆分和比较 Word 文件;添加、更新和提取 Excel 行
  • 对 Word 和 Excel 文件进行水印和密码保护
探索 Word 和 Excel
条形码

通过条形码读取、创建和拆分。

生成并嵌入 QR、Code 128、Data Matrix 和 50 多种条形码类型,从 PDF 和图像中读取它们,或按条形码分隔符拆分文档批次。

  • 创建并嵌入 50 多种条形码类型,包括 QR 码和 Data Matrix 码。
  • 从 PDF 和图像中读取条形码和 Swiss QR 代码
  • Swiss QR 和 EPC/SEPA 账单,以及按条形码拆分的 PDF 账单
探索条形码
信任与安全

您的文件安全无虞。

隐私和安全是内置的,而非后期添加的。无论您是在浏览器中转换一个文件,还是通过 API 处理数百万个文件,瑞士公司 PDF4me 都采用相同的数据处理规则。

处理后删除的文件

上传的文件在隔离的云存储中静态加密,然后通过在线工具在一小时内自动删除,或在 API 任务完成后几分钟内删除。分享链接会在 14 天后过期。我们只保留您明确保存到“我的文档”中的内容。

从未用于训练人工智能

您的文档内容绝不会用于模型训练,无论是我们自己的还是其他任何机构的。AI 数据提取仅基于您在请求时定义的模式运行。我们不会挖掘、分析或转售您发送给我们的文件。

数据处理协议

需要采购或 GDPR 审查所需的数据处理协议 (DPA)?下载 PDF4me 协议及其处理和安全附件。无需等待销售团队的回复,即可将其分享给法务部门。

端到端加密

所有浏览器上传、API 调用和下载都通过 HTTPS/TLS 传输。文件在处理过程中始终保持加密状态,租户间隔离,并且仅在作业期间保留在内存中。

ISO 27001 正在进行中In progress

我们正在实施正式的 ISO/IEC 27001 信息安全管理体系:文件化的策略、基于角色的访问控制、变更控制、事件响应和供应商监督,并计划在 2026 年进行独立审计。

专为生产而打造

一个平台即可满足日常 PDF 工具和生产环境 REST API 的需求,并提供 SDK、自动化连接器(Make、Zapier、Power Automate、n8n)和 MCP。无需自行构建 PDF 技术栈,即可将文档功能集成到您的产品中。

PDF 工具

所有 PDF 工具,一键即可使用。

在浏览器中即可免费转换、编辑、合并、压缩、签名和进行OCR识别。无需安装,无需注册。

浏览所有 33 工具 这里的每个工具同时也是一个API。
定价

只为处理的数据付费。

免费使用 API,然后充值永不过期的预付费套餐。一次性付款,无合约限制,所有套餐均可叠加到月度计划中。

试用一下
$25一度

500 API 调用

每次通话费用约为 $0.050。积分永不过期。

  • 一次性付款
  • 任何计划的堆叠
  • 每份文件一次调用
获取 500 包
小型项目
$45一度

1,000 API 调用

每次通话费用约为 $0.045。积分永不过期。

  • 一次性付款
  • 任何计划的堆叠
  • 每份文件一次调用
获取 1,000 包
用于轻自动化
$95一度

2,500 API 调用

每次通话费用约为 $0.038。积分永不过期。

  • 一次性付款
  • 任何计划的堆叠
  • 每份文件一次调用
获取 2,500 包
扩展团队
$330一度

10,000 API 调用

每次通话费用约为 $0.033。积分永不过期。

  • 一次性付款
  • 任何计划的堆叠
  • 每份文件一次调用
获取 10,000 包
大批量
$650一度

25,000 API 调用

每次通话费用约为 $0.026。积分永不过期。

  • 一次性付款
  • 任何计划的堆叠
  • 每份文件一次调用
获取 25,000 包
大批量
风俗联系销售

超过 25,000 次 API 调用

需要比我们最大预付费套餐更多的选择?请联系销售部门,了解定制批量价格、计费条款和专属支持。

  • 批量购买可享折扣
  • 专属客户支持
  • 灵活的计费方式
联系销售人员

每个文档仅需一次 API 调用。高质量转换、OCR 和 AI 功能每页计为一次调用。需要包月套餐或免费套餐?所有信息都列在完整定价页面。

查看完整定价和 API 调用费用

把你的文件准备好。

您可以使用 API 将其集成到您的产品中,无需编写代码即可实现自动化,或者使用浏览器中的免费工具。无需信用卡即可开始。

从一次通话开始curl -X POST https://api.pdf4me.com/v1/ConvertToPdf

免费版 · 无需信用卡