UNPKG

legal-markdown-js

Version:

Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version

194 lines 6.79 kB
/** * PDF Generation Module for Legal Markdown Documents * * This module provides functionality to convert processed Legal Markdown content * into professional PDF documents using Puppeteer for HTML-to-PDF conversion. * It builds upon the HTML generator to create print-ready documents with * customizable formatting, page layouts, and styling options. * * Features: * - HTML-to-PDF conversion using Puppeteer * - Multiple page formats (A4, Letter, Legal) * - Customizable margins and page orientation * - Header and footer template support * - Field highlighting for document review * - Temporary file management and cleanup * - Error handling and logging * - Dual PDF generation (normal and highlighted versions) * * @example * ```typescript * import { pdfGenerator } from './pdf-generator'; * * const pdf = await pdfGenerator.generatePdf(markdownContent, './output.pdf', { * format: 'A4', * landscape: false, * includeHighlighting: true, * margin: { top: '2cm', bottom: '2cm' } * }); * ``` */ import { HtmlGeneratorOptions } from './html-generator'; /** * Configuration options for PDF generation * * @interface PdfGeneratorOptions * @extends HtmlGeneratorOptions */ export interface PdfGeneratorOptions extends HtmlGeneratorOptions { /** Page format for the PDF */ format?: 'A4' | 'Letter' | 'Legal'; /** Whether to use landscape orientation */ landscape?: boolean; /** Page margins configuration */ margin?: { /** Top margin (e.g., '1cm', '0.5in') */ top?: string; /** Right margin (e.g., '1cm', '0.5in') */ right?: string; /** Bottom margin (e.g., '1cm', '0.5in') */ bottom?: string; /** Left margin (e.g., '1cm', '0.5in') */ left?: string; }; /** Whether to display header and footer */ displayHeaderFooter?: boolean; /** HTML template for page headers */ headerTemplate?: string; /** HTML template for page footers */ footerTemplate?: string; /** Whether to print background colors and images */ printBackground?: boolean; /** Whether to prefer CSS page size over format option */ preferCSSPageSize?: boolean; /** Path to CSS file for automatic logo detection */ cssPath?: string; } /** * PDF Generator for Legal Markdown Documents * * Converts processed Legal Markdown content into PDF documents using Puppeteer * for HTML-to-PDF conversion. Provides comprehensive formatting options and * supports both normal and highlighted document versions. * * @class PdfGenerator * @example * ```typescript * const generator = new PdfGenerator(); * const pdf = await generator.generatePdf(content, './output.pdf', { * format: 'A4', * includeHighlighting: true * }); * ``` */ export declare class PdfGenerator { private puppeteerOptions; /** * Creates a new PDF generator instance * */ constructor(); /** * Attempts to find Chrome executable on different platforms * @private */ private getChromeExecutable; /** * Attempts to find system Chrome executable * @private */ private getSystemChromeExecutable; /** * Attempts to find Puppeteer Chrome executable from cache * @private */ private getPuppeteerChromeExecutable; /** * Ensures Chrome is available for Puppeteer, installing it if necessary * @private */ private ensureChrome; /** * Gets the first available Puppeteer cache directory * @private */ private getAvailablePuppeteerCache; /** * Checks if Puppeteer has a Chrome cache available * @private */ private hasChromiumCache; /** * Generates a PDF document from Legal Markdown content * * This method orchestrates the complete PDF generation process: * 1. Converts markdown to HTML using the HTML generator * 2. Creates a temporary HTML file for Puppeteer * 3. Launches a headless Chrome browser * 4. Loads the HTML and generates PDF with specified options * 5. Cleans up temporary files and browser resources * * @param {string} markdownContent - The processed Legal Markdown content * @param {string} outputPath - Path where the PDF will be saved * @param {PdfGeneratorOptions} [options={}] - Configuration options for PDF generation * @returns {Promise<Buffer>} A promise that resolves to the PDF buffer * @throws {Error} When PDF generation fails due to browser, file system, or processing errors * @example * ```typescript * const pdf = await generator.generatePdf( * markdownContent, * './contract.pdf', * { * format: 'A4', * landscape: false, * includeHighlighting: true, * margin: { top: '2cm', bottom: '2cm' } * } * ); * ``` */ generatePdf(markdownContent: string, outputPath: string, options?: PdfGeneratorOptions): Promise<Buffer>; /** * Generate two PDF versions: one normal and one with highlighting * * This method creates two PDF versions of the same document: * 1. A normal version without field highlighting * 2. A highlighted version with field annotations * * This is useful for document review processes where both clean and * annotated versions are needed for different purposes. * * @param {string} markdownContent - The processed Legal Markdown content * @param {string} outputPath - Base path for PDF files (will be modified for each version) * @param {PdfGeneratorOptions} [options={}] - Configuration options for PDF generation * @returns {Promise<Object>} A promise that resolves to both PDF buffers * @returns {Buffer} returns.normal - The normal PDF without highlighting * @returns {Buffer} returns.highlighted - The highlighted PDF with field annotations * @throws {Error} When PDF generation fails for either version * @example * ```typescript * const { normal, highlighted } = await generator.generatePdfVersions( * markdownContent, * './contract.pdf', * { format: 'A4' } * ); * // Creates: contract.normal.pdf and contract.highlighted.pdf * ``` */ generatePdfVersions(markdownContent: string, outputPath: string, options?: PdfGeneratorOptions): Promise<{ normal: Buffer; highlighted: Buffer; }>; } /** * Singleton instance of PdfGenerator for convenient importing * * {PdfGenerator} pdfGenerator * @example * ```typescript * import { pdfGenerator } from './pdf-generator'; * const pdf = await pdfGenerator.generatePdf(content, './output.pdf'); * ``` */ export declare const pdfGenerator: PdfGenerator; //# sourceMappingURL=pdf-generator.d.ts.map