Untitled

 avatar
unknown
plain_text
a year ago
4.4 kB
10
Indexable
// import * as fs from 'fs';
// import * as path from 'path';
// import { getDocument, GlobalWorkerOptions, PDFPageProxy } from 'pdfjs-dist';
// import { createCanvas, Canvas, CanvasRenderingContext2D } from 'canvas';
// import sharp from 'sharp';
// import { Block, BoundingBox } from '@aws-sdk/client-textract';

// // Set up PDF.js worker
// GlobalWorkerOptions.workerSrc = `pdfjs-dist/build/pdf.worker.js`;

// interface CroppedImage {
//   buffer: Buffer;
//   boundingBox: BoundingBox;
//   pageNumber: number;
//   confidence: number;
//   id?: string;
// }

// class NodeCanvasFactory {
//   create(width: number, height: number): { canvas: Canvas; context: CanvasRenderingContext2D } {
//     const canvas = createCanvas(width, height);
//     const context = canvas.getContext('2d');
//     return {
//       canvas: canvas,
//       context: context,
//     };
//   }
  
//   reset(canvasAndContext: { canvas: Canvas; context: CanvasRenderingContext2D }, width: number, height: number): void {
//     canvasAndContext.canvas.width = width;
//     canvasAndContext.canvas.height = height;
//   }
  
//   destroy(canvasAndContext: { canvas: Canvas; context: CanvasRenderingContext2D }): void {
//     canvasAndContext.canvas.width = 0;
//     canvasAndContext.canvas.height = 0;
//   }
// }


// /**
//  * Crops an area from a PDF page based on Textract bounding box data and returns it as a Buffer.
//  * @param pdfPath The file path to the source PDF.
//  * @param pageNumber The page number to crop from (1-based index).
//  * @param box The BoundingBox object from the Textract response.
//  * @returns A Promise that resolves with the image Buffer in PNG format.
//  */
// export async function ImageCropService(
//     pdfPath: string,
//     pageNumber: number,
//     box: BoundingBox
//   ): Promise<Buffer> {
//     const data = new Uint8Array(fs.readFileSync(pdfPath));
//     const pdfDocument = await getDocument(data).promise;
//     const page: PDFPageProxy = await pdfDocument.getPage(pageNumber);
  
//     // 1. Render the entire PDF page to a high-resolution canvas
//     const scale = 3.0; // High scale for better quality
//     const viewport = page.getViewport({ scale });
//     const canvasFactory = new NodeCanvasFactory();
//     const { canvas, context } = canvasFactory.create(viewport.width, viewport.height);
//     await page.render({ canvasContext: context as any, viewport: viewport, canvasFactory: canvasFactory }).promise;
//     const fullPageCanvas = canvas;
  
//     // 2. Calculate the pixel coordinates for cropping
//     const cropX = box.Left! * fullPageCanvas.width;
//     const cropY = box.Top! * fullPageCanvas.height;
//     const cropWidth = box.Width! * fullPageCanvas.width;
//     const cropHeight = box.Height! * fullPageCanvas.height;
  
//     // 3. Create a new canvas and draw the cropped section onto it
//     const croppedCanvas = createCanvas(cropWidth, cropHeight);
//     const croppedContext = croppedCanvas.getContext('2d');
//     croppedContext.drawImage(
//       fullPageCanvas,
//       cropX, cropY, cropWidth, cropHeight, // Source rectangle
//       0, 0, cropWidth, cropHeight          // Destination rectangle
//     );
  
//     // 4. Return the cropped canvas as a buffer
//     return croppedCanvas.toBuffer('image/png');
//   }
  
// //   // --- Example Usage ---
  
// //   // This is the bounding box from your Textract response
// //   const figureBoundingBox: BoundingBox = {
// //     Height: 0.060031693428754807,
// //     Left: 0.8278493881225586,
// //     Top: 0.06583899259567261,
// //     Width: 0.14272360503673553,
// //   };
  
// //   const pdfFilePath = './my_document.pdf'; // <-- Make sure this file exists
// //   const pageIndex = 1;
  
// //   // Call the function and handle the returned buffer
// //   ImageCropService(pdfFilePath, pageIndex, figureBoundingBox)
// //     .then((imageBuffer) => {
// //       console.log(`✅ Successfully created image buffer.`);
// //       console.log(`Buffer size: ${imageBuffer.length} bytes.`);
// //       // Now you can send this 'imageBuffer' to an LLM API or another service.
// //       // For example, you might convert it to base64:
// //       // const base64Image = imageBuffer.toString('base64');
// //     })
// //     .catch((error) => {
// //       console.error("Failed to create image buffer:", error);
// //     });
Editor is loading...
Leave a Comment