Skip to content

k.utils.converter ​

调用 Kooboo 转换服务(HTML/PDF、Office、GIF 等)

概述 ​

k.utils.converter 通过 Kooboo 云端转换服务处理文档,依赖 UrlSetting.Converter 服务可用。

htmlToPdf() ​

将 HTML 转为 PDF 字节数组;可选 appendTexts 页眉页脚配置。

参数类型必填说明
htmlcodestring是要转换的 HTML 字符串
appendTextsAppendText[]否页眉、页脚等追加文字配置

返回: number[],PDF 文件字节。

ts
const pdf = k.utils.converter.htmlToPdf("<div>Hello</div>")
k.file.writeBinary("out/report.pdf", pdf)

gifToWebp() ​

GIF 转 WebP,参数为 GIF 字节数组。

参数类型必填说明
gifnumber[]是GIF 图片字节

返回: number[],WebP 图片字节。

ts
const gif = k.file.readBinary("media/banner.gif")
const webp = k.utils.converter.gifToWebp(gif)

k.file.writeBinary("media/banner.webp", webp)
return k.file.get("media/banner.webp")

officeToHTML() ​

上传 Office 文件字节,返回 HTML 字符串;关联资源会同步进站点数据库。

参数类型必填说明
officebytesnumber[]是Office 文件字节
filenamestring是原始文件名,需要包含扩展名

返回: string,转换后的 HTML。

ts
const html = k.utils.converter.officeToHTML(file.bytes, file.fileName)

officeToCleanHTML() ​

与 officeToHTML 类似,输出经清理的 HTML(具体规则以运行时为准)。

参数类型必填说明
officebytesnumber[]是Office 文件字节
filenamestring是原始文件名,需要包含扩展名

返回: string,转换并清理后的 HTML。

ts
const file = k.request.files[0]
const cleanHtml = k.utils.converter.officeToCleanHTML(file.bytes, file.fileName)

k.file.write("imports/imported-document.html", cleanHtml)

相关文档 ​