Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression automatique des fichiers lors d'extraction de pages #127

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ function($f3) {
$compressionType = '/ebook';
} elseif ($compressionType === 'low') {
$compressionType = '/printer';
} elseif ($compressionType === 'highest') {
$compressionType = '/prepress';
} else {
$compressionType = '/screen';
}

$arrayPath = array_keys($files);
$filePath = reset($arrayPath);

Expand Down
26 changes: 24 additions & 2 deletions public/js/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,31 @@ async function save(order) {
pdf.addPage(pdfPage);
}
const newPDF = new Blob([await pdf.save()], {type: "application/pdf"});
await download(newPDF, filename+".pdf");
//PDF-lib's PDF are quite huge, because they are uncompressed. As a result, a single page extraded from a PDF is usually (almost) as big as the whole PDF !
// And PDF-lib doesn't have built-in compression, so we'll use the compress endpoint
// Let's try to compress our PDF using the compress feature
let data = new FormData();
data.append("input_pdf_upload", newPDF, filename + ".pdf");
data.append("compressionType", "highest");
const response = await fetch("/compress", {method: "POST", body: data});
let compressedBlob = null;
if (response.status == 200) {
compressedBlob = await response.blob();
}
// Should not happen, but worst case scenario just send the uncompressed file
else {
await download(newPDF, filename+".pdf");
return;
}
// If there is any kind of error (e.g file already optimized), /compress will answer with a 200 + a blob of HTML (to be displayed on the /compress page)
// We obviously don't want to send HTML as PDF, so make sure we are sending the correct thing
if (compressedBlob.type == "application/pdf") {
await download(compressedBlob, filename+".pdf");
}
else {
await download(newPDF, filename+".pdf");
}
}

function createEventsListener() {
document.getElementById('save-select_mobile').addEventListener('click', async function(event) {
event.preventDefault();
Expand Down