news 2026/8/1 21:11:11

如何集成Resend?用htmldocs自动生成并发送PDF邮件附件

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
如何集成Resend?用htmldocs自动生成并发送PDF邮件附件

如何集成Resend?用htmldocs自动生成并发送PDF邮件附件

【免费下载链接】htmldocsThe modern alternative to LaTeX. Create PDF documents templates using React, JSX, and Tailwind项目地址: https://gitcode.com/gh_mirrors/ht/htmldocs

htmldocs是一款基于React、JSX和Tailwind的现代化PDF文档模板创建工具,作为LaTeX的理想替代品,它能帮助用户轻松生成专业的PDF文件。本文将详细介绍如何将Resend与htmldocs集成,实现PDF邮件附件的自动生成与发送,让你的文档处理流程更加高效便捷。

集成Resend前的准备工作 📋

在开始集成Resend之前,我们需要完成以下几个关键步骤,为后续的操作做好充分准备。

1. 发布发票模板到htmldocs

首先,你需要将准备好的发票模板发布到htmldocs平台。这一步是实现PDF生成的基础,具体操作可以参考官方文档中的发布到云端指南,按照其中的步骤完成模板的发布。

2. 安装Resend依赖

接下来,我们需要安装Resend的相关依赖。根据你所使用的包管理器,在项目根目录下执行以下命令之一:

npm install resend
pnpm install resend
yarn install resend
bun install resend

3. 配置Resend API密钥

Resend的使用需要API密钥的支持,你需要在Resend官网注册账号并获取API密钥。然后,在你的项目环境中设置该密钥,你可以在项目的环境变量文件中添加如下配置:

RESEND_API_KEY=your_resend_api_key

将其中的your_resend_api_key替换为你实际获取到的Resend API密钥。

在htmldocs中集成Resend的实现步骤 🚀

完成了前期的准备工作后,我们就可以开始在htmldocs中集成Resend,实现PDF邮件附件的自动生成与发送了。

1. 导入Resend并初始化

首先,在你的代码文件中导入Resend模块,并使用之前配置的API密钥进行初始化。代码如下:

import { Resend } from 'resend'; const resend = new Resend(process.env.RESEND_API_KEY);

2. 定义相关接口类型

为了确保数据的规范性和类型安全,我们需要定义一些与发票相关的接口类型。这些接口将帮助我们更好地组织和传递数据。

interface BilledTo { name: string; address: string; city: string; state: string; zip: string; phone: string; } interface YourCompany { name: string; address: string; city: string; state: string; zip: string; taxId: string; phone: string; email: string; } interface Service { name: string; description?: string; quantity: number; rate: number; }

3. 编写发送发票邮件的函数

接下来,我们编写一个用于发送发票邮件的异步函数。这个函数将完成PDF的生成和邮件的发送两个核心操作。

首先,通过htmldocs的API生成PDF文档。我们需要向htmldocs的API端点发送POST请求,传递必要的参数,包括文档格式和相关属性。

async function sendInvoiceEmail() { try { // 1. Generate the invoice using htmldocs API const response = await fetch('https://api.htmldocs.com/api/documents/invoice', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.HTMLDOCS_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ format: 'pdf', props: { billedTo: { name: "Acme Corp", address: "123 Business Ave", city: "San Francisco", state: "CA", zip: "94107", phone: "555-0123" }, yourCompany: { name: "Your Company", address: "456 Banana Rd.", city: "San Francisco", state: "CA", zip: "94107", taxId: "00XXXXX1234X0XX", phone: "123-456-7890", email: "hello@email.com" }, services: [ { name: "Premium License", description: "Annual subscription", quantity: 1, rate: 999.00 } ] } }) });

在这个请求中,我们需要提供htmldocs的API密钥进行身份验证,同时指定生成的文档格式为PDF,并传递发票的相关属性,如客户信息、公司信息和服务项目等。

如果请求成功,我们将获取到生成的PDF文档的二进制数据。然后,使用Resend发送包含该PDF附件的邮件。

if (!response.ok) { const errorData = await response.json().catch(() => null); throw new Error( `Failed to generate invoice: ${response.status} ${response.statusText}${ errorData ? ` - ${JSON.stringify(errorData)}` : '' }` ); } const documentBuffer = await response.arrayBuffer(); // 2. Send email with the generated invoice const data = await resend.emails.send({ from: 'you@yourdomain.com', to: 'recipient@example.com', subject: 'Invoice for Your Recent Services', attachments: [ { content: Buffer.from(documentBuffer), filename: 'invoice.pdf', }, ], html: ` <div style="font-family: sans-serif; max-width: 600px; margin: 0 auto;"> <h2 style="color: #1a1a1a;">Your Invoice</h2> <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> Thank you for your business. Please find your invoice attached to this email. </p> <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> Payment is due within 15 days from the invoice date. For your convenience, payment instructions are included in the invoice. </p> <p style="color: #4a4a4a; font-size: 16px; line-height: 1.5;"> If you have any questions about this invoice, please contact our billing department using the contact information provided in the invoice. </p> <hr style="border: none; border-top: 1px solid #e1e1e1; margin: 30px 0;" /> <p style="color: #898989; font-size: 14px;"> This is an automated message. Please do not reply to this email. </p> </div> `, }); console.log('Email sent successfully:', data); } catch (error) { if (error instanceof Error) { console.error('Error:', error.message); } else { console.error('Unknown error:', error); } throw error; // Re-throw to handle it in the calling code } }

在发送邮件时,我们需要指定发件人、收件人、邮件主题、附件以及邮件的HTML内容。其中,附件就是我们刚刚生成的PDF文档,我们将其转换为Buffer格式并添加到邮件中。

4. 使用htmldocs编辑器创建和编辑模板

htmldocs提供了直观易用的编辑器,让你可以轻松创建和编辑PDF模板。你可以在编辑器中选择各种预设模板,如简历、发票、书籍等,并根据自己的需求进行自定义修改。

上图展示的是htmldocs编辑器的简历模板编辑界面,你可以看到左侧是模板列表,右侧是预览区域,你可以在编辑区对模板内容进行修改,实时查看效果。

5. 运行开发服务器预览效果

在开发过程中,你可以运行htmldocs的开发服务器,实时预览你的模板效果。开发服务器会自动监测文件的变化并进行热重载,让你的开发效率更高。

如上图所示,开发服务器运行后,你可以在浏览器中查看模板的实时预览效果,方便你进行调整和优化。

总结

通过本文的介绍,你已经了解了如何将Resend与htmldocs集成,实现PDF邮件附件的自动生成与发送。从前期的准备工作到具体的实现步骤,我们详细讲解了每一个环节,希望能帮助你顺利完成集成。

如果你想了解更多关于文档生成的信息,可以参考htmldocs的API参考文档。通过Resend和htmldocs的强大功能,你可以轻松构建高效、专业的文档处理流程,提升工作效率。

现在,就动手尝试将Resend集成到你的htmldocs项目中,体验自动生成和发送PDF邮件附件的便捷吧!

【免费下载链接】htmldocsThe modern alternative to LaTeX. Create PDF documents templates using React, JSX, and Tailwind项目地址: https://gitcode.com/gh_mirrors/ht/htmldocs

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/1 21:04:36

PL-2303芯片Windows 10驱动终极解决方案:3步搞定停产硬件兼容性

PL-2303芯片Windows 10驱动终极解决方案&#xff1a;3步搞定停产硬件兼容性 【免费下载链接】pl2303-win10 Windows 10 driver for end-of-life PL-2303 chipsets. 项目地址: https://gitcode.com/gh_mirrors/pl/pl2303-win10 还在为Windows 10系统上PL-2303旧版芯片的兼…

作者头像 李华
网站建设 2026/8/1 21:03:11

计算机网络期末高效复习指南:四步刷题法与核心知识点精讲

1. 项目概述&#xff1a;一份期末复习题的深度价值又到期末季&#xff0c;看着《计算机网络》这门课&#xff0c;是不是感觉协议、模型、地址、算法像一团乱麻&#xff0c;背了又忘&#xff0c;做题就懵&#xff1f;我当年也是这么过来的。后来我发现&#xff0c;单纯刷题背答案…

作者头像 李华
网站建设 2026/8/1 21:01:33

【信息科学与工程学】【数据中心】——第三十五篇 操作系统如何满足云原生/虚拟机/容器化02

编号 类型 领域 系统 Scale Up/Out/Across/云原生/虚拟化/容器化/其他 场景+问题(含系统模块/组件/结构和层次化分析) 问题的数学分析(拓扑学/代数/图论/优化/排队论/控制理论/信息论/计算机系统架构等) 参数列表及参数的数值范围设计 关联知识 201 Chiplet互连的…

作者头像 李华