Convertire file XLS con immagini o grafici in PDF con JavaScript via C++
Contents
[
Hide
]
Aspose.Cells supporta la conversione di file XLS contenenti immagini e grafici in documenti PDF. Aspose.Cells for JavaScript via C++ può funzionare autonomamente per convertire un foglio di calcolo in PDF: non è necessario Aspose.PDF per JavaScript via C++ per la conversione. Il processo può essere eseguito in memoria in quanto non dipende da file XML temporanei o intermedi. Ciò significa che grandi file Excel, ad esempio quelli contenenti immagini, grafici e altri oggetti grafici, possono essere convertiti rapidamente ed efficientemente.
Codice di Esempio
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert Excel to PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<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, Utils } = 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();
// Instantiating a Workbook object by opening the Excel file from the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save the workbook as PDF
const outputData = workbook.save(SaveFormat.Pdf);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Output.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>
Se il foglio di calcolo contiene formule, è meglio chiamare il metodo Workbook.calculateFormula() proprio prima di renderizzare in PDF. In questo modo si garantisce che i valori dipendenti dalle formule siano ricalcolati e i valori corretti siano visualizzati nel PDF.