File size: 8,302 Bytes
8ee53dc 9bb556d 8ee53dc bc26067 8ee53dc f1312dc 8ee53dc 9bb556d 8ee53dc bc26067 1a26ae1 bc26067 8ee53dc 1a26ae1 bc26067 1a26ae1 8ee53dc bc26067 8ee53dc 1a26ae1 8ee53dc bc26067 1a26ae1 bc26067 1a26ae1 bc26067 1a26ae1 bc26067 1a26ae1 bc26067 1a26ae1 bc26067 1a26ae1 bc26067 1a26ae1 bc26067 8ee53dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Images to PDF</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
text-align: center;
touch-action: none; /* Prevent default touch behaviors like scrolling */
}
#imageContainer {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
margin: 20px 0;
padding: 10px;
border: 1px solid #ccc;
}
.imageWrapper {
max-width: 200px;
cursor: move; /* For desktop */
position: relative;
user-select: none; /* Prevent text selection during drag/touch */
}
.imageWrapper img {
max-width: 100%;
height: auto;
border: 1px solid #ddd;
border-radius: 5px;
}
.imageWrapper.dragging {
opacity: 0.5; /* Visual feedback during drag/touch */
}
.imageWrapper.over {
border: 2px dashed #4CAF50; /* Visual feedback for drop target */
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Images to PDF Converter</h1>
<input type="file" id="imageInput" accept="image/*" multiple>
<p>Drag and drop (desktop) or tap and move (mobile) images to reorder them for the PDF.</p>
<div id="imageContainer"></div>
<button onclick="generatePDF()">Download as PDF</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script>
const { jsPDF } = window.jspdf;
const imageInput = document.getElementById('imageInput');
const imageContainer = document.getElementById('imageContainer');
let images = [];
// Handle image uploads
imageInput.addEventListener('change', (event) => {
images = [];
imageContainer.innerHTML = '';
const files = Array.from(event.target.files);
files.forEach((file) => {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.src = e.target.result;
img.onload = () => {
images.push({ src: e.target.result, width: img.width, height: img.height });
renderImages();
};
};
reader.readAsDataURL(file);
});
});
// Render images with drag-and-drop and touch functionality
function renderImages() {
imageContainer.innerHTML = '';
images.forEach((image, index) => {
const wrapper = document.createElement('div');
wrapper.className = 'imageWrapper';
wrapper.draggable = true;
wrapper.dataset.index = index;
const img = new Image();
img.src = image.src;
wrapper.appendChild(img);
// Desktop drag-and-drop
wrapper.addEventListener('dragstart', (e) => {
wrapper.classList.add('dragging');
e.dataTransfer.setData('text/plain', index);
});
wrapper.addEventListener('dragend', () => {
wrapper.classList.remove('dragging');
clearOverStyles();
});
wrapper.addEventListener('dragover', (e) => {
e.preventDefault();
wrapper.classList.add('over');
});
wrapper.addEventListener('dragleave', () => {
wrapper.classList.remove('over');
});
wrapper.addEventListener('drop', (e) => {
e.preventDefault();
const draggedIndex = parseInt(e.dataTransfer.getData('text/plain'));
const dropIndex = parseInt(wrapper.dataset.index);
reorderImages(draggedIndex, dropIndex);
renderImages();
});
// Touch events for mobile
wrapper.addEventListener('touchstart', (e) => {
e.preventDefault();
wrapper.classList.add('dragging');
startTouchReorder(e, wrapper, index);
});
wrapper.addEventListener('touchmove', (e) => {
e.preventDefault();
handleTouchMove(e, wrapper);
});
wrapper.addEventListener('touchend', (e) => {
e.preventDefault();
wrapper.classList.remove('dragging');
endTouchReorder(wrapper, index);
clearOverStyles();
});
imageContainer.appendChild(wrapper);
});
}
// Touch reordering logic
let touchStartY = 0;
let targetIndex = null;
function startTouchReorder(event, wrapper, index) {
const touch = event.touches[0];
touchStartY = touch.clientY;
targetIndex = index;
}
function handleTouchMove(event, wrapper) {
const touch = event.touches[0];
const touchY = touch.clientY;
// Find the element under the touch point
const elements = document.elementsFromPoint(touch.clientX, touchY);
const targetWrapper = elements.find(el => el.classList.contains('imageWrapper') && el !== wrapper);
// Clear previous 'over' styles
clearOverStyles();
if (targetWrapper) {
targetWrapper.classList.add('over');
}
}
function endTouchReorder(wrapper, startIndex) {
const overElement = document.querySelector('.imageWrapper.over');
if (overElement) {
const dropIndex = parseInt(overElement.dataset.index);
reorderImages(startIndex, dropIndex);
renderImages();
}
}
function clearOverStyles() {
document.querySelectorAll('.imageWrapper.over').forEach(el => el.classList.remove('over'));
}
function reorderImages(draggedIndex, dropIndex) {
if (draggedIndex !== dropIndex) {
const [draggedImage] = images.splice(draggedIndex, 1);
images.splice(dropIndex, 0, draggedImage);
}
}
// Generate PDF from images
function generatePDF() {
if (images.length === 0) {
alert('Please upload at least one image.');
return;
}
const doc = new jsPDF();
const pageWidth = doc.internal.pageSize.getWidth();
const pageHeight = doc.internal.pageSize.getHeight();
const margin = 10;
const maxImgWidth = pageWidth - 2 * margin;
const maxImgHeight = pageHeight - 2 * margin;
images.forEach((image, index) => {
if (index > 0) {
doc.addPage();
}
// Calculate scaling to fit image within page
let imgWidth = image.width;
let imgHeight = image.height;
const ratio = Math.min(maxImgWidth / imgWidth, maxImgHeight / imgHeight);
imgWidth = imgWidth * ratio;
imgHeight = imgHeight * ratio;
// Center the image on the page
const x = (pageWidth - imgWidth) / 2;
const y = (pageHeight - imgHeight) / 2;
doc.addImage(image.src, 'JPEG', x, y, imgWidth, imgHeight);
});
doc.save('images.pdf');
}
</script>
</body>
</html> |