k.utils.converter
Call Kooboo conversion services (HTML/PDF, Office, GIF, etc.)
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, background jobs | Runtime environment must reach Kooboo cloud conversion endpoints | Generating PDFs from HTML, converting GIFs to WebP, and Office-to-HTML import |
k.utils.converter processes documents through the Kooboo cloud conversion service and requires network connectivity to that service.
Quick Start
js
k.api.post("exportPdf", () => {
const pdf = k.utils.converter.htmlToPdf("<h1>Order Receipt</h1><p>Paid</p>")
k.file.writeBinary("out/receipt.pdf", pdf)
return { bytes: pdf.length }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
htmlToPdf(htmlcode[, appendTexts]) | Convert HTML to PDF bytes with optional header/footer text | number[] |
gifToWebp(gif) | Convert GIF image bytes to WebP image bytes | number[] |
officeToHTML(officebytes, filename) | Convert Office document bytes to HTML and sync assets | string |
officeToCleanHTML(officebytes, filename) | Convert Office document bytes to HTML with inline styles stripped | string |
htmlToPdf()
Convert HTML to a PDF byte array, with optional appendTexts header and footer overlays.
| Parameter | Type | Required | Description |
|---|---|---|---|
htmlcode | string | Yes | HTML string to convert |
appendTexts | AppendText[] | No | Header, footer, or other appended text configuration (text, color: { r, g, b }, location: { x, y, align }) |
Returns: number[], PDF file bytes.
js
const pdf = k.utils.converter.htmlToPdf("<div>Hello</div>", [{
text: "Kooboo",
color: { r: 255, g: 0, b: 0 },
location: { x: 10, y: 10, align: "left" }
}])
k.file.writeBinary("out/report.pdf", pdf)gifToWebp()
Convert GIF image bytes to WebP bytes.
| Parameter | Type | Required | Description |
|---|---|---|---|
gif | number[] | Yes | GIF image bytes |
Returns: number[], WebP image bytes.
js
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()
Upload Office file bytes and return an HTML string; associated resources are synchronized into the site database.
| Parameter | Type | Required | Description |
|---|---|---|---|
officebytes | number[] | Yes | Office file bytes |
filename | string | Yes | Original file name, including extension |
Returns: string, converted HTML.
js
const html = k.utils.converter.officeToHTML(file.bytes, file.fileName)officeToCleanHTML()
Similar to officeToHTML, converts Office bytes to HTML and strips inline body styles from the output.
| Parameter | Type | Required | Description |
|---|---|---|---|
officebytes | number[] | Yes | Office file bytes |
filename | string | Yes | Original file name, including extension |
Returns: string, converted and cleaned HTML.
js
const file = k.request.files[0]
const cleanHtml = k.utils.converter.officeToCleanHTML(file.bytes, file.fileName)
k.file.write("imports/imported-document.html", cleanHtml)TypeScript Definitions
ts
interface Converter {
htmlToPdf(htmlcode: string, appendTexts?: AppendText[]): number[];
gifToWebp(gif: number[]): number[];
officeToHTML(officebytes: number[], filename: string): string;
officeToCleanHTML(officebytes: number[], filename: string): string;
}
interface AppendText {
text: string;
location: Location;
color: Color;
}
interface Location {
x: number;
y: number;
align: string;
}
interface Color {
r: number;
g: number;
b: number;
}Related Docs
- k.utils.office — Local Excel reading and writing
- k.file