mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-10-25 11:17:28 +02:00 
			
		
		
		
	Added endpoint: scale-pages
This commit is contained in:
		
							parent
							
								
									7503d70377
								
							
						
					
					
						commit
						facf1553ec
					
				| @ -2,6 +2,7 @@ | |||||||
| import Operations from '../../utils/pdf-operations'; | import Operations from '../../utils/pdf-operations'; | ||||||
| import { respondWithPdfFile, respondWithPdfFiles, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils'; | import { respondWithPdfFile, respondWithPdfFiles, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils'; | ||||||
| import { PdfFile, PdfFileSchema } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile' | import { PdfFile, PdfFileSchema } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile' | ||||||
|  | import { ScalePageSchema } from '@stirling-pdf/shared-operations/src/functions/scalePage' | ||||||
| 
 | 
 | ||||||
| import express, { Request, Response, RequestHandler } from 'express'; | import express, { Request, Response, RequestHandler } from 'express'; | ||||||
| const router = express.Router(); | const router = express.Router(); | ||||||
| @ -84,7 +85,8 @@ registerEndpoint("/impose", "", upload.single("file"), Operations.impose, Joi.ob | |||||||
|     format: Joi.string().required(), |     format: Joi.string().required(), | ||||||
| }).required()); | }).required()); | ||||||
| 
 | 
 | ||||||
| //Adjust page size/scale
 | registerEndpoint("/scale-pages", "", upload.single("file"), Operations.scalePage, ScalePageSchema.required()); | ||||||
|  | 
 | ||||||
| //Auto Split Pages
 | //Auto Split Pages
 | ||||||
| //Adjust Colours/Contrast
 | //Adjust Colours/Contrast
 | ||||||
| //Crop
 | //Crop
 | ||||||
|  | |||||||
| @ -1,6 +1,32 @@ | |||||||
| 
 | 
 | ||||||
|  | import Joi from 'joi'; | ||||||
| import { PDFPage } from 'pdf-lib'; | import { PDFPage } from 'pdf-lib'; | ||||||
| import { PdfFile, RepresentationType } from '../wrappers/PdfFile'; | import { PdfFile, RepresentationType, PdfFileSchema } from '../wrappers/PdfFile'; | ||||||
|  | 
 | ||||||
|  | const whSchema = Joi.string().custom((value, helpers) => { | ||||||
|  |     console.log("value.pageSize", typeof value) | ||||||
|  |     try { | ||||||
|  |         const obj = JSON.parse(value); | ||||||
|  |         if (!obj.width && !obj.height) { | ||||||
|  |             return helpers.error('any.required', { message: 'At least one of width/height must be present' }); | ||||||
|  |         } | ||||||
|  |         if (typeof obj.width != 'number' && typeof obj.width != 'undefined') { | ||||||
|  |             return helpers.error('any.invalid', { message: 'Width must be a number if present' }); | ||||||
|  |         } | ||||||
|  |         if (typeof obj.height != 'number' && typeof obj.height != 'undefined') { | ||||||
|  |             return helpers.error('any.invalid', { message: 'Height must be a number if present' }); | ||||||
|  |         } | ||||||
|  |         return obj; | ||||||
|  |     } catch (error) { | ||||||
|  |         return helpers.error('any.invalid', { message: 'Value must be a valid JSON' }); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | export const ScalePageSchema = Joi.object({ | ||||||
|  |     file: PdfFileSchema.required(), | ||||||
|  |     pageSize: Joi.alternatives().try(whSchema, Joi.array().items(whSchema)).required(), | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| export type ScalePageParamsType = { | export type ScalePageParamsType = { | ||||||
|     file: PdfFile; |     file: PdfFile; | ||||||
| @ -29,10 +55,10 @@ export async function scalePage(params: ScalePageParamsType): Promise<PdfFile> { | |||||||
| 
 | 
 | ||||||
| function resize(page: PDFPage, newSize: {width?:number,height?:number}) { | function resize(page: PDFPage, newSize: {width?:number,height?:number}) { | ||||||
|     const calculatedSize = calculateSize(page, newSize); |     const calculatedSize = calculateSize(page, newSize); | ||||||
|     page.setSize(calculatedSize.width, calculatedSize.height); |  | ||||||
| 
 |  | ||||||
|     const xRatio = calculatedSize.width / page.getWidth(); |     const xRatio = calculatedSize.width / page.getWidth(); | ||||||
|     const yRatio = calculatedSize.height / page.getHeight(); |     const yRatio = calculatedSize.height / page.getHeight(); | ||||||
|  | 
 | ||||||
|  |     page.setSize(calculatedSize.width, calculatedSize.height); | ||||||
|     page.scaleContent(xRatio, yRatio); |     page.scaleContent(xRatio, yRatio); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user