mirror of
https://github.com/ParisNeo/lollms-webui.git
synced 2024-12-21 13:17:47 +00:00
122 lines
3.1 KiB
JavaScript
122 lines
3.1 KiB
JavaScript
class LollmsMarkdown2PDF {
|
|
constructor() {
|
|
this.markdown = '';
|
|
this.pdfDoc = null;
|
|
}
|
|
|
|
// Load Markdown content
|
|
loadMarkdown(markdown) {
|
|
this.markdown = markdown;
|
|
}
|
|
|
|
// Parse Markdown and convert to PDF
|
|
async convertToPDF() {
|
|
// We'll use the pdfkit library for PDF generation
|
|
const PDFDocument = require('pdfkit');
|
|
const fs = require('fs');
|
|
const showdown = require('showdown');
|
|
const cheerio = require('cheerio');
|
|
|
|
this.pdfDoc = new PDFDocument();
|
|
|
|
// Convert Markdown to HTML
|
|
const converter = new showdown.Converter({tables: true, strikethrough: true});
|
|
const html = converter.makeHtml(this.markdown);
|
|
|
|
// Parse HTML
|
|
const $ = cheerio.load(html);
|
|
|
|
// Process elements
|
|
$('body').children().each((i, elem) => {
|
|
this.processElement($(elem));
|
|
});
|
|
|
|
return this.pdfDoc;
|
|
}
|
|
|
|
// Process individual elements
|
|
processElement(elem) {
|
|
switch(elem.get(0).tagName.toLowerCase()) {
|
|
case 'h1':
|
|
case 'h2':
|
|
case 'h3':
|
|
case 'h4':
|
|
case 'h5':
|
|
case 'h6':
|
|
this.addHeading(elem);
|
|
break;
|
|
case 'p':
|
|
this.addParagraph(elem);
|
|
break;
|
|
case 'img':
|
|
this.addImage(elem);
|
|
break;
|
|
case 'table':
|
|
this.addTable(elem);
|
|
break;
|
|
case 'pre':
|
|
this.addCodeBlock(elem);
|
|
break;
|
|
// Add more cases for other elements
|
|
}
|
|
}
|
|
|
|
// Add heading
|
|
addHeading(elem) {
|
|
const level = parseInt(elem.get(0).tagName.slice(1));
|
|
const fontSize = 24 - (level - 1) * 2;
|
|
this.pdfDoc.fontSize(fontSize).text(elem.text(), {bold: true});
|
|
this.pdfDoc.moveDown();
|
|
}
|
|
|
|
// Add paragraph
|
|
addParagraph(elem) {
|
|
this.pdfDoc.fontSize(12).text(elem.text());
|
|
this.pdfDoc.moveDown();
|
|
}
|
|
|
|
// Add image
|
|
addImage(elem) {
|
|
const src = elem.attr('src');
|
|
if (src) {
|
|
this.pdfDoc.image(src, {fit: [400, 300], align: 'center'});
|
|
this.pdfDoc.moveDown();
|
|
}
|
|
}
|
|
|
|
// Add table
|
|
addTable(elem) {
|
|
const rows = elem.find('tr').map((i, row) => {
|
|
return $(row).find('td, th').map((j, cell) => {
|
|
return $(cell).text().trim();
|
|
}).get();
|
|
}).get();
|
|
|
|
this.pdfDoc.table(rows, {
|
|
prepareHeader: () => this.pdfDoc.font('Helvetica-Bold'),
|
|
prepareRow: (row, i) => this.pdfDoc.font('Helvetica').fontSize(12)
|
|
});
|
|
this.pdfDoc.moveDown();
|
|
}
|
|
|
|
// Add code block
|
|
addCodeBlock(elem) {
|
|
const code = elem.text();
|
|
this.pdfDoc.font('Courier').fontSize(10).text(code);
|
|
this.pdfDoc.moveDown();
|
|
}
|
|
|
|
// Save PDF to file
|
|
savePDF(outputPath) {
|
|
return new Promise((resolve, reject) => {
|
|
const stream = fs.createWriteStream(outputPath);
|
|
this.pdfDoc.pipe(stream);
|
|
this.pdfDoc.end();
|
|
stream.on('finish', resolve);
|
|
stream.on('error', reject);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = LollmsMarkdown2PDF;
|
|
|