Skip to content

Commit 2d5901f

Browse files
committed
remove compressImage in image2pdf to improve pdf quaility
1 parent 50c106a commit 2d5901f

1 file changed

Lines changed: 34 additions & 55 deletions

File tree

src/views/image2pdf.vue

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,7 @@ const handleFileRemove = (uploadFile, uploadFiles) => {
9595
}
9696
};
9797
98-
// Image compression utility (optional, but good for performance)
99-
const compressImage = (img, maxWidth, maxHeight, quality = 0.7) => {
100-
return new Promise((resolve) => {
101-
const canvas = document.createElement("canvas");
102-
const ctx = canvas.getContext("2d");
103-
let { width, height } = img;
104-
105-
// Calculate scaling
106-
if (width > maxWidth || height > maxHeight) {
107-
const ratio = Math.min(maxWidth / width, maxHeight / height);
108-
width *= ratio;
109-
height *= ratio;
110-
}
111-
112-
canvas.width = width;
113-
canvas.height = height;
114-
ctx.drawImage(img, 0, 0, width, height);
115-
116-
// Get blob with specified quality
117-
canvas.toBlob((blob) => resolve(blob), "image/jpeg", quality);
118-
});
119-
};
120-
98+
// Image compression removed to prefer quality
12199
const generatePDF = async () => {
122100
if (rawFiles.value.length === 0) {
123101
ElMessage.warning($t ? $t('imageToPdf.warningNoImages') : "Please upload at least one image.");
@@ -140,53 +118,54 @@ const generatePDF = async () => {
140118
for (let i = 0; i < rawFiles.value.length; i++) {
141119
const file = rawFiles.value[i];
142120
const img = new Image();
143-
img.src = URL.createObjectURL(file); // Use object URL from raw file
121+
const objectUrl = URL.createObjectURL(file);
122+
img.src = objectUrl; // Use object URL from raw file
144123
145124
await new Promise((resolve, reject) => {
146-
img.onload = async () => {
125+
img.onload = () => {
147126
try {
148-
// Optional: Compress image before adding to PDF
149-
// Adjust maxWidth/maxHeight as needed, potentially based on PDF page size
150-
const compressedBlob = await compressImage(img, pdfWidth * 3, pdfHeight * 3, 0.7); // Higher res for better quality PDF
151-
const reader = new FileReader();
152-
153-
reader.readAsDataURL(compressedBlob);
154-
reader.onloadend = () => {
155-
const compressedImgDataUrl = reader.result;
156-
const imgProps = pdf.getImageProperties(compressedImgDataUrl);
127+
const imgWidth = img.width;
128+
const imgHeight = img.height;
129+
const ratio = Math.min(pdfWidth / imgWidth, pdfHeight / imgHeight);
157130
158-
// Calculate image dimensions to fit A4 page, maintaining aspect ratio
159-
const imgWidth = imgProps.width;
160-
const imgHeight = imgProps.height;
161-
const ratio = Math.min(pdfWidth / imgWidth, pdfHeight / imgHeight);
131+
const displayWidth = imgWidth * ratio;
132+
const displayHeight = imgHeight * ratio;
162133
163-
const displayWidth = imgWidth * ratio;
164-
const displayHeight = imgHeight * ratio;
134+
// Center image on page
135+
const x = (pdfWidth - displayWidth) / 2;
136+
const y = (pdfHeight - displayHeight) / 2;
165137
166-
// Center image on page (optional)
167-
const x = (pdfWidth - displayWidth) / 2;
168-
const y = (pdfHeight - displayHeight) / 2;
138+
if (i > 0) pdf.addPage();
169139
170-
171-
if (i > 0) pdf.addPage();
172-
pdf.addImage(compressedImgDataUrl, "JPEG", x, y, displayWidth, displayHeight);
173-
174-
URL.revokeObjectURL(img.src); // Clean up object URL
140+
// Read the original file directly as Data URL
141+
const reader = new FileReader();
142+
reader.readAsDataURL(file);
143+
reader.onloadend = () => {
144+
const imgDataUrl = reader.result;
145+
146+
// Determine format based on file type
147+
let format = 'JPEG';
148+
if (file.type === 'image/png') format = 'PNG';
149+
else if (file.type === 'image/webp') format = 'WEBP';
150+
151+
pdf.addImage(imgDataUrl, format, x, y, displayWidth, displayHeight);
152+
153+
URL.revokeObjectURL(objectUrl); // Clean up object URL
175154
resolve();
176155
};
177156
reader.onerror = (e) => {
178-
URL.revokeObjectURL(img.src);
157+
URL.revokeObjectURL(objectUrl);
179158
console.error("FileReader error:", e);
180-
reject(new Error(`Failed to read compressed image for ${file.name}`));
159+
reject(new Error(`Failed to read image for ${file.name}`));
181160
};
182-
} catch (compressError) {
183-
URL.revokeObjectURL(img.src);
184-
console.error("Image compression error:", compressError);
185-
reject(new Error(`Failed to compress image ${file.name}`));
161+
} catch (err) {
162+
URL.revokeObjectURL(objectUrl);
163+
console.error("PDF add image error:", err);
164+
reject(new Error(`Failed to add image ${file.name} to PDF`));
186165
}
187166
};
188167
img.onerror = (e) => {
189-
URL.revokeObjectURL(img.src); // Clean up object URL
168+
URL.revokeObjectURL(objectUrl); // Clean up object URL
190169
console.error("Image load error:", e);
191170
reject(new Error(`Failed to load image ${file.name}`));
192171
};

0 commit comments

Comments
 (0)