0% found this document useful (0 votes)
28 views2 pages

Try PDF

The document contains code to split a PDF file uploaded by the user into separate PDF files based on page ranges specified by the user. It uses a FileReader to read the PDF file and create blobs for each page range slice to generate download links to the individual split PDF files.

Uploaded by

phoenix guru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Try PDF

The document contains code to split a PDF file uploaded by the user into separate PDF files based on page ranges specified by the user. It uses a FileReader to read the PDF file and create blobs for each page range slice to generate download links to the individual split PDF files.

Uploaded by

phoenix guru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Splitter</title>
</head>
<body>
<h2>Upload PDF:</h2>
<input type="file" id="pdfFile" accept=".pdf"><br><br>

<h2>Enter Page Ranges:</h2>


<input type="text" id="pageRanges" placeholder="Enter page ranges to
split"><br><br>

<button onclick="splitPDF()">Split PDF</button><br><br>

<div id="downloadLinks"></div>

<script>
function splitPDF() {
var fileInput = document.getElementById('pdfFile');
var pageRangesInput = document.getElementById('pageRanges');
var downloadLinks = document.getElementById('downloadLinks');

var file = fileInput.files[0];


var pageRanges = pageRangesInput.value.split(',');

// Assume pageWidth and pageHeight are obtained from the server-side


processing
var pageWidth = 595; // Example width in points (8.27 inches)
var pageHeight = 842; // Example height in points (11.69 inches)

for (var i = 0; i < pageRanges.length; i++) {


var range = pageRanges[i];
var startEnd = range.split('-');
var start = parseInt(startEnd[0]);
var end = startEnd.length > 1 ? parseInt(startEnd[1]) : start;

var reader = new FileReader();


reader.onload = function(event) {
var uint8Array = new Uint8Array(event.target.result);
var blob = new Blob([uint8Array], { type: 'application/pdf' });
var url = window.URL.createObjectURL(blob);

var link = document.createElement('a');


var serialNumber = downloadLinks.children.length + 1;
link.href = url;
link.download = 'filename_' + serialNumber + '.pdf';
link.textContent = 'Download PDF (filename_' + serialNumber +
')';
link.style.display = 'block';

downloadLinks.appendChild(link);
};
// Adjust slice based on page width and height
var sliceStart = (start - 1) * (pageWidth * pageHeight / 72); //
Convert points to bytes
var sliceEnd = end * (pageWidth * pageHeight / 72);
reader.readAsArrayBuffer(file.slice(sliceStart, sliceEnd));
}
}
</script>
</body>
</html>

You might also like