latex-to-pdf
v1.0.37
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
class LatexToPdf {
constructor(reportType: string);
populate<T>(data: T, output: string): string;
}Methods:
constructor(reportType: string): Initializes the generator with a report type. The type must be pre-generated usinggenerateAllTypes.populate<T>(data: T, output: string): string: Injects data into the LaTeX template and generates a PDF.
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 pdfPath = pdfGenerator.populate(data, "./output/test.pdf");
console.log(`Generated PDF at: ${pdfPath}`);
License
© 2025 Tyto Insights. All Rights Reserved.
