// const poppler = require('pdf-poppler');
// const sharp = require('sharp');
// const path = require('path');
// const fs = require('fs-extra');
// const axios = require('axios');
// const https = require('https');

// // URL to the input PDF file
// const pdfUrl = 'https://apps.cbe.com.et:100/?id=FT25045Q83LT13955253';

// // Directory to save the downloaded PDF and output images
// const outputDir = 'backend/uploads/';

// // Function to get a unique filename using a timestamp
// const getUniqueFilename = () => {
//     const timestamp = Date.now();
//     return `downloaded_${timestamp}.pdf`;
// };

// // Generate a unique filename for the downloaded PDF
// const pdfPath = path.join(outputDir, getUniqueFilename());

// // Create an Https Agent to ignore SSL certificate verification errors
// const httpsAgent = new https.Agent({
//     rejectUnauthorized: false,
// });

// // Function to download PDF from URL
// const downloadPdf = async (url, outputPath) => {
//     const response = await axios({
//         url,
//         method: 'GET',
//         responseType: 'stream',
//         httpsAgent, // Use the custom https agent
//     });

//     await new Promise((resolve, reject) => {
//         response.data.pipe(fs.createWriteStream(outputPath))
//             .on('finish', resolve)
//             .on('error', reject);
//     });

//     console.log(`Downloaded PDF: ${outputPath}`);
// };

// // Function to convert PDF to JPEG
// const convertPdfToJpeg = async (pdfPath, outputDir) => {
//     try {
//         const options = {
//             format: 'jpeg',
//             out_dir: outputDir,
//             out_prefix: path.basename(pdfPath, path.extname(pdfPath)),
//             page: 1
//         };
        
//         // Convert PDF to images
//         await poppler.convert(pdfPath, options);
//  // Get the output image path
//  const outputPath = path.join(outputDir, `${path.basename(pdfPath, path.extname(pdfPath))}-01.jpg`);
//  console.log(`Converted image: ${outputPath}`);
 
//  return outputPath;
//     } catch (error) {
//         console.error('Error converting PDF to JPEG:', error);
//     }
// };

// // Download the PDF and convert it to JPEG
// exports.processPdf = async () => {
//     try {
//         // Ensure the output directory exists
//         await fs.ensureDir(outputDir);

//         // Download the PDF
//         await downloadPdf(pdfUrl, pdfPath);

//         // Convert the downloaded PDF to JPEG
//         await convertPdfToJpeg(pdfPath, outputDir);
//     } catch (error) {
//         console.error('Error processing PDF:', error);
//     }
// };

