Fit All Worksheet Columns on Single PDF Page with JavaScript via C++
Contents
[
Hide
]
Sometimes you want to generate a PDF file that fits all a worksheet’s columns onto one page. The PdfSaveOptions.allColumnsInOnePagePerSheet property provides this feature in a very easy-to-use manner. Complex calculations such as the height and width of the output PDF are handled internally and are based on the data in the worksheet.
Fit Worksheet Columns on Single PDF Page
PdfSaveOptions.allColumnsInOnePagePerSheet ensures that all columns in a worksheet are rendered to a single PDF page, although rows may expand to several pages depending on the data in the worksheet.
The sample code below shows how to use PdfSaveOptions.allColumnsInOnePagePerSheet property to render a large worksheet of 100 columns.
<!DOCTYPE html>
<html>
<head>
<title>Save Workbook to PDF Example</title>
</head>
<body>
<h1>Save Workbook to PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Run Example</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, PdfSaveOptions, 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');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Create and initialize an instance of Workbook
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create and initialize an instance of PdfSaveOptions
const saveOptions = new PdfSaveOptions();
// Set AllColumnsInOnePagePerSheet to true (converted from setter to property)
saveOptions.allColumnsInOnePagePerSheet = true;
// Save Workbook to PDF format by passing the object of PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, saveOptions);
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';
resultDiv.innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the file.</p>';
});
</script>
</html>
When a given worksheet has many columns, the rendered PDF file may show the content in a very small size. It is still readable when scaled up in a viewing application such as Acrobat Reader.
If your spreadsheet contains formulas, it is best to call Workbook.calculateFormula() just before rendering the spreadsheet to PDF format. Doing so will ensure that the formula dependent values are recalculated, and the correct values are rendered in the PDF.