Skip to content

Commit

Permalink
JPG verarbeitung im PDF eingefügt.
Browse files Browse the repository at this point in the history
Dateitypen erkennung eingebaut.
Uploadpfad für Lieder und Liturgie korrigiert.
  • Loading branch information
Simon Luthe authored and Simon Luthe committed Jul 23, 2024
1 parent 2fafad3 commit 35d2b5b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
12 changes: 8 additions & 4 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ async function startServer() {
console.log('Received files:', req.files);

const { typ, titel, inhalt, strophen, copyright } = req.body;
const notenbild = req.files && req.files['notenbild'] ? req.files['notenbild'][0].path : null;
const notenbildMitText = req.files && req.files['notenbildMitText'] ? req.files['notenbildMitText'][0].path : null;
const notenbild = req.files && req.files['notenbild']
? path.join('/uploads', path.relative(path.join(__dirname, 'uploads'), req.files['notenbild'][0].path))
: null;
const notenbildMitText = req.files && req.files['notenbildMitText']
? path.join('/uploads', path.relative(path.join(__dirname, 'uploads'), req.files['notenbildMitText'][0].path))
: null;

console.log('Prepared data:', { typ, titel, inhalt, strophen, notenbild, notenbildMitText, copyright });

Expand Down Expand Up @@ -231,10 +235,10 @@ async function startServer() {
let notenbildMitText = existingObjekt[0].notenbildMitText;

if (req.files && req.files['notenbild']) {
notenbild = req.files['notenbild'][0].path;
notenbild = path.join('/uploads', path.relative(path.join(__dirname, 'uploads'), req.files['notenbild'][0].path));
}
if (req.files && req.files['notenbildMitText']) {
notenbildMitText = req.files['notenbildMitText'][0].path;
notenbildMitText = path.join('/uploads', path.relative(path.join(__dirname, 'uploads'), req.files['notenbildMitText'][0].path));
}

const query = 'UPDATE objekte SET typ = ?, titel = ?, inhalt = ?, strophen = ?, notenbild = ?, notenbildMitText = ?, copyright = ? WHERE id = ?';
Expand Down
50 changes: 48 additions & 2 deletions frontend/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,16 @@ async function generatePDF(format) {
const logoResponse = await fetch(logoUrl);
if (!logoResponse.ok) throw new Error(`HTTP error! Status: ${logoResponse.status}`);
const logoArrayBuffer = await logoResponse.arrayBuffer();
logoImage = await doc.embedPng(logoArrayBuffer);

const logoType = getImageType(logoArrayBuffer);

if (logoType === 'png') {
logoImage = await doc.embedPng(logoArrayBuffer);
} else if (logoType === 'jpeg') {
logoImage = await doc.embedJpg(logoArrayBuffer);
} else {
throw new Error('Unsupported logo image type');
}

console.log("Church logo embedded successfully");
} catch (error) {
Expand All @@ -1881,6 +1890,20 @@ async function generatePDF(format) {
console.log("No church logo path found in global config");
}

function getImageType(arrayBuffer) {
const uint8Array = new Uint8Array(arrayBuffer);
const pngSignature = [137, 80, 78, 71, 13, 10, 26, 10];
const jpegSignature = [255, 216, 255];

if (pngSignature.every((byte, index) => uint8Array[index] === byte)) {
return 'png';
} else if (jpegSignature.every((byte, index) => uint8Array[index] === byte)) {
return 'jpeg';
} else {
return 'unknown';
}
}

function addLogoToPage(page) {
if (logoImage) {
const pageWidth = page.getWidth();
Expand Down Expand Up @@ -2015,7 +2038,16 @@ async function generatePDF(format) {
try {
const response = await fetch(imgSrc);
const imgArrayBuffer = await response.arrayBuffer();
let img = await doc.embedPng(imgArrayBuffer);
const imgType = getImageType(imgArrayBuffer);

let img;
if (imgType === 'png') {
img = await doc.embedPng(imgArrayBuffer);
} else if (imgType === 'jpeg') {
img = await doc.embedJpg(imgArrayBuffer);
} else {
throw new Error('Unsupported image type');
}

const scaledDims = img.scale(imgWidth / img.width);

Expand All @@ -2033,6 +2065,20 @@ async function generatePDF(format) {
}
}

function getImageType(arrayBuffer) {
const uint8Array = new Uint8Array(arrayBuffer);
const pngSignature = [137, 80, 78, 71, 13, 10, 26, 10];
const jpegSignature = [255, 216, 255];

if (pngSignature.every((byte, index) => uint8Array[index] === byte)) {
return 'png';
} else if (jpegSignature.every((byte, index) => uint8Array[index] === byte)) {
return 'jpeg';
} else {
return 'unknown';
}
}

async function drawIcon(iconName, x, y, size) {
console.log("Drawing icon:", { iconName, x, y, size });
const iconPaths = {
Expand Down

0 comments on commit 35d2b5b

Please sign in to comment.