Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: fixed pdf as crazy image generation, replaced basic html2canvas with better pdf generation

hashedviking4146c5931b55
diff --git a/static/js/app.js b/static/js/app.js
index 22ddaf4ea9..c462757987 100644
--- a/static/js/app.js
+++ b/static/js/app.js
@@ -1407,7 +1407,6 @@ document.addEventListener('DOMContentLoaded', () => {
const generateTextBasedPDF = async () => {
try {
// Get all text elements and handle them differently than images and special content
- // Use html2canvas only for complex elements that can't be easily converted to text
const elements = Array.from(contentClone.children);
let currentY = margin;
let pageNum = 1;
@@ -1424,15 +1423,19 @@ document.addEventListener('DOMContentLoaded', () => {
addPageWithHeader(pageNum);
- // Process each element - for better quality combine text-based approach with canvas
+ // Process each element
for (const element of elements) {
- // Simple text content can be handled directly by jsPDF
- if (element.tagName === 'P' && !element.querySelector('img, canvas, svg, code, pre')) {
- // Extract and add text content directly
+ // Simple text content - handled directly by jsPDF
+ if ((element.tagName === 'P' || element.tagName === 'DIV') &&
+ !element.querySelector('img, canvas, svg') &&
+ element.children.length === 0) {
+
pdf.setFontSize(11);
pdf.setTextColor(0, 0, 0);
const text = element.textContent.trim();
+ if (!text) continue; // Skip empty text
+
const textLines = pdf.splitTextToSize(text, contentWidth);
// Check if we need a new page
@@ -1445,9 +1448,8 @@ document.addEventListener('DOMContentLoaded', () => {
pdf.text(textLines, margin, currentY + 12);
currentY += (textLines.length * 14) + 10;
}
- // Handle headings differently to maintain hierarchy
+ // Handle headings
else if (['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(element.tagName)) {
- // Extract and add heading text directly
const fontSize = {
'H1': 24,
'H2': 20,
@@ -1457,10 +1459,21 @@ document.addEventListener('DOMContentLoaded', () => {
'H6': 11
}[element.tagName];
+ // Add heading text as native PDF text
pdf.setFontSize(fontSize);
- pdf.setTextColor(0, 0, 0);
+
+ // Use a different color for headings to match styling
+ if (element.tagName === 'H1') {
+ pdf.setTextColor(110, 79, 246); // Purple for main headers
+ } else if (element.tagName === 'H2') {
+ pdf.setTextColor(70, 90, 150); // Darker blue for H2
+ } else {
+ pdf.setTextColor(0, 0, 0); // Black for other headings
+ }
const text = element.textContent.trim();
+ if (!text) continue; // Skip empty headings
+
const textLines = pdf.splitTextToSize(text, contentWidth);
// Check if we need a new page
@@ -1472,29 +1485,277 @@ document.addEventListener('DOMContentLoaded', () => {
pdf.text(textLines, margin, currentY + fontSize);
currentY += (textLines.length * (fontSize + 4)) + 10;
+
+ // Add a subtle underline for H1 and H2
+ if (element.tagName === 'H1' || element.tagName === 'H2') {
+ pdf.setDrawColor(110, 79, 246, 0.5);
+ pdf.setLineWidth(0.5);
+ pdf.line(
+ margin,
+ currentY - 5,
+ margin + Math.min(contentWidth, pdf.getTextWidth(text) * 1.2),
+ currentY - 5
+ );
+ currentY += 5; // Add a bit more space after underlined headings
+ }
+ }
+ // Handle lists
+ else if (element.tagName === 'UL' || element.tagName === 'OL') {
+ pdf.setFontSize(11);
+ pdf.setTextColor(0, 0, 0);
+
+ const listItems = element.querySelectorAll('li');
+ let itemNumber = 1;
+
+ for (const item of listItems) {
+ const prefix = element.tagName === 'UL' ? '• ' : `${itemNumber}. `;
+ const text = item.textContent.trim();
+
+ if (!text) continue; // Skip empty list items
+
+ // Split text to fit width, accounting for bullet/number indent
+ const textLines = pdf.splitTextToSize(text, contentWidth - 15);
+
+ // Check if we need a new page
+ if (currentY + (textLines.length * 14) > pdfHeight - margin) {
+ pageNum++;
+ addPageWithHeader(pageNum);
+ currentY = margin;
+ }
+
+ // Add the bullet/number
+ pdf.text(prefix, margin, currentY + 12);
+
+ // Add the text with indent
+ pdf.text(textLines, margin + 15, currentY + 12);
+ currentY += (textLines.length * 14) + 5;
+
+ if (element.tagName === 'OL') itemNumber++;
+ }
+
+ currentY += 5; // Extra space after list
+ }
+ // Handle code blocks as text
+ else if (element.tagName === 'PRE' || element.querySelector('pre')) {
+ const codeElement = element.tagName === 'PRE' ? element : element.querySelector('pre');
+ const codeText = codeElement.textContent.trim();
+
+ if (!codeText) continue; // Skip empty code blocks
+
+ // Use monospace font for code
+ pdf.setFont("courier", "normal");
+ pdf.setFontSize(9); // Smaller font for code
+
+ // Calculate code block size
+ const codeLines = codeText.split('\n');
+ const lineHeight = 10; // Smaller line height for code
+ const codeBlockHeight = (codeLines.length * lineHeight) + 20; // Add padding
+
+ // Add a background for the code block
+ if (currentY + codeBlockHeight > pdfHeight - margin) {
+ pageNum++;
+ addPageWithHeader(pageNum);
+ currentY = margin;
+ }
+
+ // Draw code block background
+ pdf.setFillColor(245, 245, 245); // Light gray background
+ pdf.rect(margin - 5, currentY, contentWidth + 10, codeBlockHeight, 'F');
+
+ // Draw a border
+ pdf.setDrawColor(220, 220, 220);
+ pdf.setLineWidth(0.5);
+ pdf.rect(margin - 5, currentY, contentWidth + 10, codeBlockHeight, 'S');
+
+ // Add the code text
+ pdf.setTextColor(0, 0, 0);
+ currentY += 10; // Add padding at top
+
+ codeLines.forEach(line => {
+ // Handle indentation by preserving leading spaces
+ const spacePadding = line.match(/^(\s*)/)[0].length;
+ const visibleLine = line.trimLeft();
+
+ // Calculate width of space character
+ const spaceWidth = pdf.getStringUnitWidth(' ') * 9 / pdf.internal.scaleFactor;
+
+ pdf.text(visibleLine, margin + (spacePadding * spaceWidth), currentY);
+ currentY += lineHeight;
+ });
+
+ currentY += 10; // Add padding at bottom
+
+ // Reset to normal font
+ pdf.setFont("helvetica", "normal");
+ pdf.setFontSize(11);
+ }
+ // Handle tables as text
+ else if (element.tagName === 'TABLE' || element.querySelector('table')) {
+ const tableElement = element.tagName === 'TABLE' ? element : element.querySelector('table');
+
+ if (!tableElement) continue;
+
+ // Get table rows
+ const rows = Array.from(tableElement.querySelectorAll('tr'));
+ if (rows.length === 0) continue;
+
+ // Calculate column widths
+ const headerCells = Array.from(rows[0].querySelectorAll('th, td'));
+ const numColumns = headerCells.length;
+
+ if (numColumns === 0) continue;
+
+ // Default column width distribution (equal)
+ const colWidth = contentWidth / numColumns;
+
+ // Start drawing table
+ let tableY = currentY + 10;
+
+ // Check if we need a new page
+ if (tableY + (rows.length * 20) > pdfHeight - margin) {
+ pageNum++;
+ addPageWithHeader(pageNum);
+ tableY = margin + 10;
+ currentY = margin;
+ }
+
+ // Draw table header
+ pdf.setFillColor(240, 240, 240);
+ pdf.rect(margin, tableY, contentWidth, 20, 'F');
+
+ pdf.setFont("helvetica", "bold");
+ pdf.setFontSize(10);
+ pdf.setTextColor(0, 0, 0);
+
+ headerCells.forEach((cell, index) => {
+ const text = cell.textContent.trim();
+ const x = margin + (index * colWidth) + 5;
+ pdf.text(text, x, tableY + 13);
+ });
+
+ // Draw horizontal line after header
+ pdf.setDrawColor(200, 200, 200);
+ pdf.setLineWidth(0.5);
+ pdf.line(margin, tableY + 20, margin + contentWidth, tableY + 20);
+
+ tableY += 20;
+
+ // Draw table rows
+ pdf.setFont("helvetica", "normal");
+ for (let i = 1; i < rows.length; i++) {
+ // Check if we need a new page
+ if (tableY + 20 > pdfHeight - margin) {
+ // Draw bottom border for last row on current page
+ pdf.line(margin, tableY, margin + contentWidth, tableY);
+
+ // Add new page
+ pageNum++;
+ addPageWithHeader(pageNum);
+ tableY = margin + 10;
+
+ // Redraw header on new page
+ pdf.setFillColor(240, 240, 240);
+ pdf.rect(margin, tableY, contentWidth, 20, 'F');
+
+ pdf.setFont("helvetica", "bold");
+ headerCells.forEach((cell, index) => {
+ const text = cell.textContent.trim();
+ const x = margin + (index * colWidth) + 5;
+ pdf.text(text, x, tableY + 13);
+ });
+
+ pdf.line(margin, tableY + 20, margin + contentWidth, tableY + 20);
+ tableY += 20;
+ pdf.setFont("helvetica", "normal");
+ }
+
+ // Get cells for this row
+ const cells = Array.from(rows[i].querySelectorAll('td, th'));
+
+ // Alternate row background for better readability
+ if (i % 2 === 0) {
+ pdf.setFillColor(250, 250, 250);
+ pdf.rect(margin, tableY, contentWidth, 20, 'F');
+ }
+
+ // Add cell content
+ cells.forEach((cell, index) => {
+ const text = cell.textContent.trim();
+ const x = margin + (index * colWidth) + 5;
+ pdf.text(text, x, tableY + 13);
+ });
+
+ // Draw horizontal line after row
+ pdf.line(margin, tableY + 20, margin + contentWidth, tableY + 20);
+ tableY += 20;
+ }
+
+ // Draw vertical lines for columns
+ for (let i = 0; i <= numColumns; i++) {
+ const x = margin + (i * colWidth);
+ pdf.line(x, currentY + 10, x, tableY);
+ }
+
+ currentY = tableY + 10;
+ }
+ // Images still need to be handled as images
+ else if (element.tagName === 'IMG' || element.querySelector('img')) {
+ const imgElement = element.tagName === 'IMG' ? element : element.querySelector('img');
+
+ if (!imgElement || !imgElement.src) continue;
+
+ try {
+ // Create a new image to get dimensions
+ const img = new Image();
+ img.src = imgElement.src;
+
+ // Calculate dimensions
+ const imgWidth = contentWidth;
+ const imgHeight = img.height * (contentWidth / img.width);
+
+ // Check if we need a new page
+ if (currentY + imgHeight > pdfHeight - margin) {
+ pageNum++;
+ addPageWithHeader(pageNum);
+ currentY = margin;
+ }
+
+ // Add image to PDF
+ pdf.addImage(img.src, 'JPEG', margin, currentY, imgWidth, imgHeight);
+ currentY += imgHeight + 10;
+ } catch (imgError) {
+ console.error('Error adding image:', imgError);
+ pdf.text("[Image could not be rendered]", margin, currentY + 12);
+ currentY += 20;
+ }
}
- // For complex elements like code, tables, images etc. use html2canvas
+ // Other complex elements still use html2canvas as fallback
else {
+ try {
const canvas = await html2canvas(element, {
- scale: 2, // Higher scale for better quality
+ scale: 2,
useCORS: true,
logging: false,
backgroundColor: '#FFFFFF'
});
const imgData = canvas.toDataURL('image/png');
+ const imgWidth = contentWidth;
const imgHeight = (canvas.height * contentWidth) / canvas.width;
- // Check if we need a new page
if (currentY + imgHeight > pdfHeight - margin) {
pageNum++;
addPageWithHeader(pageNum);
currentY = margin;
}
- // Add image to PDF
- pdf.addImage(imgData, 'PNG', margin, currentY, contentWidth, imgHeight);
+ pdf.addImage(imgData, 'PNG', margin, currentY, imgWidth, imgHeight);
currentY += imgHeight + 10;
+ } catch (canvasError) {
+ console.error('Error rendering complex element:', canvasError);
+ pdf.text("[Complex content could not be rendered]", margin, currentY + 12);
+ currentY += 20;
+ }
}
}