Skip to content

Commit

Permalink
pdf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
enoy19 committed Aug 25, 2023
1 parent 8d824d6 commit e13e88b
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 78 deletions.
38 changes: 0 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"dependencies": {
"gm": "^1.25.0",
"neat-csv": "^7.0.0",
"pdf-lib": "^1.17.1",
"serialport": "^11.0.1",
"svelte-local-storage-store": "^0.5.0",
"throttle-debounce": "^5.0.0"
Expand Down
37 changes: 2 additions & 35 deletions src/lib/server/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PDFDocument } from 'pdf-lib';
import { convertPdfToImage } from './pdf';
import { identify, imageMagick } from './imageMagick';

Expand Down Expand Up @@ -59,37 +58,9 @@ export async function imageToZpl(

export async function pdfToImage(buffer: Buffer | ArrayBuffer, dpi = 300) {
const pdfDataBuffer = Buffer.from(buffer);
const image = await convertPdfToImage(pdfDataBuffer, 1, 10);

const pdfDoc = await PDFDocument.load(pdfDataBuffer);
const page = pdfDoc.getPage(0);
const { width: widthInPoints, height: heightInPoints } = page.getSize();
const rotation = page.getRotation();

const { width: widthInPointsRotated, height: heightInPointsRotated } = rotateDimension(
{ width: widthInPoints, height: heightInPoints },
rotation.type === 'degrees' ? rotation.angle : toDegree(rotation.angle)
);

const width = Math.round((widthInPointsRotated / 72) * dpi);
const height = Math.round((heightInPointsRotated / 72) * dpi);

const image = await convertPdfToImage(pdfDataBuffer, 1, 10, width, height);

return { image, width, height };
}

function rotateDimension({ width, height }: Dimensions, rotationDegrees: number) {
if (rotationDegrees % 180 !== 0) {
return {
width: height,
height: width
};
}

return {
width,
height
};
return image;
}

function bufferToBitBuffer(inputBuffer: Buffer): Buffer {
Expand Down Expand Up @@ -139,10 +110,6 @@ function calculateRotation(imageSize: Dimensions, pageSize: Dimensions): number
return imageIsLandscape !== pageIsLandscape ? 90 : 0;
}

function toDegree(radian: number) {
return (radian * 180) / Math.PI;
}

function isDecimal(num: number) {
return num % 1 !== 0;
}
Expand Down
3 changes: 0 additions & 3 deletions src/lib/server/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ export async function convertPdfToImage(
buffer: Buffer,
page: number,
dpmm: number,
width: number,
height: number
) {
const pageSetup = `pdf[${page - 1}]`;

return new Promise<Buffer>((resolve, reject) => {
imageMagick(buffer, pageSetup)
.in('-define', 'pdf:use-cropbox=true')
.density(dpmmToDpi(dpmm), dpmmToDpi(dpmm))
.resize(width, height, '!')
.quality(0)
.compress('jpeg')
.stream('png', (error, stdout) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/[printer]/pdf/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const POST: RequestHandler = async ({ params, request }) => {

const { width, height } = mmToPixels(printer.dpmm, printer.widthMm, printer.heightMm);

const { image } = await pdfToImage(Buffer.from(await request.arrayBuffer()));
const image = await pdfToImage(Buffer.from(await request.arrayBuffer()));
const zpl = await imageToZpl(image, width, height);

await print(zpl, printerIdentifier);
Expand Down

0 comments on commit e13e88b

Please sign in to comment.