Renderizar una página PDF por hoja de Excel Conversión de Excel a PDF con JavaScript vía C++
Contents
[
Hide
]
Cuando se trabaja con archivos grandes de Microsoft Excel (por ejemplo, un libro que tiene muchas hojas, cada una con 50 columnas y 300 o más filas de datos), puede que desees que la salida en PDF muestre una página por hoja, independientemente del tamaño de la hoja. Esto significa que cada página tendrá probablemente un tamaño radicalmente diferente. Esto se puede lograr usando Aspose.Cells for JavaScript vía C++.
Consulte el siguiente código de ejemplo que convierte un archivo de Excel con varias hojas de cálculo a PDF.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Excel to PDF</title>
</head>
<body>
<h1>Convert Excel to PDF (One Page Per Worksheet)</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Convert to PDF</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, PdfSaveOptions } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Initialize a new Workbook by opening the selected Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Implement one page per worksheet option
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.onePagePerSheet = true;
// Save the PDF file (returns binary data)
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'OutputFile.out.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF generated successfully! Click the download link to get the PDF file.</p>';
});
</script>
</html>
Si la opción PdfSaveOptions.onePagePerSheet está configurada a true, todo el contenido de la hoja será renderizado en una sola página PDF.
Si tu hoja de cálculo contiene fórmulas, es mejor llamar a Workbook.calculateFormula() justo antes de renderizar la hoja en PDF. Esto asegura que los valores dependientes de las fórmulas se recalculen y que se muestren los valores correctos en el PDF.