Skip to content

PDF / TeX Rendering

DieselEngine includes a TeX rendering engine for generating PDFs from LaTeX templates. It also supports generic template merging with variable substitution.

Template Merging

diesel.mergeTemplateToString(templatePath, context)

Merge variables into a template file, returning the result as a string. Variables in the template are replaced with values from the context object.

  • Parameters:
    • templatePath: string — path to the template file on the host filesystem
    • context: object — key-value pairs for variable substitution
  • Returns: string — the merged content
javascript
const html = diesel.mergeTemplateToString(
  "/tmp/dieselfuse/templates/welcome-email.html",
  {
    name: "Alice",
    company: "Acme Corp",
    activationLink: "https://example.com/activate/abc123"
  }
);

diesel.mergeTemplate(templatePath, context)

Merge variables into a template file, returning a JavaFile reference.

  • Parameters: templatePath: string, context: object
  • Returns: JavaFile
javascript
const file = diesel.mergeTemplate(
  "/tmp/dieselfuse/templates/report.tex",
  { title: "Q1 Report", author: "Alice", year: "2026" }
);
console.log("Merged file at:", file.getAbsolutePath());

PDF Generation

diesel.renderTemplateToPdf(templatePath, assets, debug)

Render a LaTeX template to a PDF file. The engine compiles the .tex file using a TeX processor, downloading any specified assets first.

  • Parameters:
    • templatePath: string — path to the .tex file
    • assets: TeXAsset[] — array of assets to download (each has sourceUrl and outputName)
    • debug: boolean — if true, keep intermediate TeX files for debugging
  • Returns: JavaFile — the generated PDF file
javascript
const pdf = diesel.renderTemplateToPdf(
  "/tmp/dieselfuse/templates/invoice.tex",
  [
    { sourceUrl: "https://example.com/logo.png", outputName: "logo.png" },
    { sourceUrl: "https://example.com/footer.png", outputName: "footer.png" }
  ],
  false
);

console.log("PDF generated:", pdf.getAbsolutePath());
console.log("Size:", pdf.length(), "bytes");

TeXAsset Object

Each asset in the assets array should have:

PropertyTypeDescription
sourceUrlstringURL to download the asset from
outputNamestringFilename to save the asset as (in the working directory)

Common Patterns

Invoice Generation

javascript
// /services/InvoiceService.js
export class InvoiceService {

  static generateInvoice(orderId) {
    const order = OrderService.getOrderById(orderId);
    const customer = CustomerService.getCustomerById(order.customerId);

    // Merge variables into the LaTeX template
    const merged = diesel.mergeTemplate(
      "/tmp/dieselfuse/templates/invoice.tex",
      {
        invoiceNumber: `INV-${order.id}`,
        customerName: customer.name,
        customerAddress: customer.address,
        date: new Date().toLocaleDateString("de-DE"),
        items: order.items.map(i => `${i.name} & ${i.qty} & ${i.price}`).join(" \\\\\n"),
        total: order.total.toFixed(2)
      }
    );

    // Render to PDF
    const pdf = diesel.renderTemplateToPdf(
      merged.getAbsolutePath(),
      [{ sourceUrl: "https://example.com/company-logo.png", outputName: "logo.png" }],
      false
    );

    return pdf;
  }
}

HTML to Email with Template

javascript
function sendReport(recipient, reportData) {
  const html = diesel.mergeTemplateToString(
    "/tmp/dieselfuse/templates/report-email.html",
    {
      recipientName: reportData.name,
      reportDate: new Date().toLocaleDateString(),
      summary: reportData.summary,
      totalOrders: String(reportData.totalOrders),
      revenue: reportData.revenue.toFixed(2)
    }
  );

  const email = diesel.getEmailBuilder()
    .from("reports@example.com")
    .to(recipient)
    .withSubject("Monthly Report - " + new Date().toLocaleDateString())
    .withHTMLText(html)
    .buildEmail();

  const mailer = diesel.getMailerBuilder("smtp.example.com", 587, "user", "pass");
  mailer.buildMailer().sendMail(email);
}

PDF as HTTP Response

javascript
export function handleRequest(context) {
  const orderId = context.getParameterAsInteger("orderId");
  const pdf = InvoiceService.generateInvoice(orderId);

  // Read the PDF file content
  const Files = Java.type('java.nio.file.Files');
  const Paths = Java.type('java.nio.file.Paths');
  const pdfBytes = Files.readAllBytes(Paths.get(pdf.getAbsolutePath()));

  return Results.ok()
    .contentType("application/pdf")
    .addHeader("Content-Disposition", `attachment; filename="invoice-${orderId}.pdf"`)
    .renderRaw(pdfBytes);
}

JavaFile API

The JavaFile object returned by rendering methods provides:

MethodReturnsDescription
.getName()stringFile name
.getPath()stringRelative path
.getAbsolutePath()stringAbsolute path
.getParent()stringParent directory path
.exists()booleanWhether the file exists
.isFile()booleanWhether it's a regular file
.length()numberFile size in bytes
.delete()booleanDelete the file

DieselEngine Scripting Documentation