Skip to content

k.utils.converter ​

Call Kooboo conversion services (HTML/PDF, Office, GIF, etc.)

Execution ContextPrerequisitesTypical Use
KScript API, background jobsRuntime environment must reach Kooboo cloud conversion endpointsGenerating 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 / PropertyDescriptionReturns
htmlToPdf(htmlcode[, appendTexts])Convert HTML to PDF bytes with optional header/footer textnumber[]
gifToWebp(gif)Convert GIF image bytes to WebP image bytesnumber[]
officeToHTML(officebytes, filename)Convert Office document bytes to HTML and sync assetsstring
officeToCleanHTML(officebytes, filename)Convert Office document bytes to HTML with inline styles strippedstring

htmlToPdf() ​

Convert HTML to a PDF byte array, with optional appendTexts header and footer overlays.

ParameterTypeRequiredDescription
htmlcodestringYesHTML string to convert
appendTextsAppendText[]NoHeader, 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.

ParameterTypeRequiredDescription
gifnumber[]YesGIF 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.

ParameterTypeRequiredDescription
officebytesnumber[]YesOffice file bytes
filenamestringYesOriginal 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.

ParameterTypeRequiredDescription
officebytesnumber[]YesOffice file bytes
filenamestringYesOriginal 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;
}