latex-to-pdf
v2.1.1
Published
**LaTeX to PDF** is a Node.js module that allows you to generate PDF reports from LaTeX templates with dynamic data injection. It provides functionalities for parsing templates, generating TypeScript interfaces for type safety, and compiling LaTeX files i
Readme
LaTeX to PDF Documentation
Overview
LaTeX to PDF is a Node.js module that allows you to generate PDF reports from LaTeX templates with dynamic data injection. It provides functionalities for parsing templates, generating TypeScript interfaces for type safety, and compiling LaTeX files into PDFs.
Dependencies
- pdflatex
- Windows install: https://www.tug.org/texlive/
- Mac install:
brew install texlive - Debian/Ubuntu:
sudo apt install texlive-latex-basethen optionallysudo apt install texlive-latex-extra texlive-fonts-recommended
Installation
Using git
Setup the package in it's own directory
git clone https://github.com/TytoLtd/latext_to_pdf_ts.git
cd latex-to-pdf
yarn install
yarn build
yarn linkLink to another project. In the other project:
yarn link latex-to-pdfUsing the package manager if I decide to publish it (haven't yet)
yarn add latex-to-pdfUsage
Generating TypeScript Interfaces from Templates
Before generating PDFs, you need to extract fields from LaTeX templates and generate TypeScript interfaces:
latex-to-pdf-setup <TEMPLATE_DIRECTORY>This command scans the provided directory for .tex templates, extracts placeholders, and generates corresponding TypeScript interfaces.
<TEMPLATE_DIRECTORY> is where you decide to keep your latex templates. An example latex template is provided below.
API Reference
LatexToPdf Class
interface CompileTexOptions {
passes?: number; // default 2
draftFirst?: boolean; // default true: first pass uses -draftmode
engine?: string; // default "pdflatex"
flags?: string[]; // extra engine flags
}
class LatexToPdf {
constructor(reportType: string);
renderTemplate<T>(data: T, outputPdfPath: string): string;
compileTex(texPath: string, options?: CompileTexOptions): Promise<PdfResult>;
populate<T>(data: T, outputPdfPath: string, options?: CompileTexOptions): Promise<PdfResult>;
}Methods:
constructor(reportType: string): Initializes the generator with a report type. The type must be pre-generated usinggenerateAllTypes.renderTemplate<T>(data: T, outputPdfPath: string): string: Renders and writes a.texfile (same path as output with.pdfreplaced by.tex) and returns the tex path.compileTex(texPath: string, options?: CompileTexOptions): Promise<PdfResult>: Compiles a.texfile to PDF with first-class multi-pass support.populate<T>(data: T, outputPdfPath: string, options?: CompileTexOptions): Promise<PdfResult>: Backward-compatible one-call helper that runsrenderTemplatethencompileTex.
generateAllTypes
function generateAllTypes(templateDir: string): string[];Scans a directory for .tex templates and generates TypeScript interfaces.
Configuration
Ensure the pdflatex command is installed and available in your system path to enable PDF generation.
See above dependency.
Example
Latex Template
\documentclass{article}
\usepackage{longtable}
\begin{document}
{{let cust1:import<Customer,"../src/Customer.ts">}}
{{let results_table:[new<date:string,item:string,price:number>]}}
{{let meeps:[import<Meep,"../src/Meep.ts">]}}
{{let date:raw<string>}}
Invoice for {{cust1.name}}
Date: {{date}}
\begin{longtable}{|l|c|c|}
\hline
\textbf{Date} & \textbf{Item} & \textbf{Price (ZAR)} \\
\hline
{{table(results_table,[date,item,price])}}
\hline
\end{longtable}
\section{Meep}
{{for meep in meeps
{\textbf{Meep ID:} {{meep.id}} \\}
}}
\section{Magic}
{{if ({{date}} == "Meep")
{\textbf{Magic:} 1 \\}else{\textbf{Not Magic:} 0 \\}
}}
\end{document}
TS Code
import { LatexToPdf } from "latex-to-pdf";
import { Customer } from "./src/Customer";
import { InvoiceData } from "./latex_interfaces/invoice";
const pdfGenerator = new LatexToPdf("invoice"); // Same as template name just without .tex. So invoice.tex -> invoice
// The data type for a template will always be called {templateName}Data.
// You can find the structure in the generated folder "node_modules/latex-to-pdf/generated"
let cust: Customer={
name: "Mikey",
id: 123
}
let data: InvoiceData={
cust1: cust,
results_table: [
{
date: "2021-01-01",
item: "Item 1",
price: 10
},
{
date: "2021-01-02",
item: "Item 2",
price: 20
}
],
meeps: [
{
id: 999
},
{
id: 9991
}
],
date: "not Meep"
}
const texPath = pdfGenerator.renderTemplate(data, "./output/test.pdf");
const result = await pdfGenerator.compileTex(texPath, {
passes: 2,
draftFirst: true
});
if (!result.success) {
throw new Error(result.message);
}
console.log(`Generated PDF at: ${result.outputPath}`);
License
© 2025 Tyto Insights. All Rights Reserved.
