•  
      tasks #90362 Create set of APIs to e-sign a PDF using Flask/Python
    #90362
    Abhijeet Anand (abhi912)
    2023-06-30 13:33
    2023-06-30 13:33
    Details
    Create set of APIs to e-sign a PDF using Flask/Python

    Create set of APIs to e-sign a PDF using Flask/Python

    API/process 1.

    1. take a html template, write cv
    2. using pypdf generate pdf
    3. store pdf

    API/process 2.

    1. create a new process, which takes signature and sign that PDF

    ------------sample code-------------- from flask import Flask, jsonify, request import subprocess from PyPDF2 import PdfReader, PdfWriter import os

    app = Flask(name)

    @app.route('/generate_certificate', methods=['GET']) def generate_certificate(): try: # Signer details signer_common_name = 'Your Name' signer_organization = 'Your Organization' signer_organization_unit = 'Your Organization Unit' signer_locality = 'Your Locality' signer_state = 'Your State' signer_country = 'Your Country Code'

        # Generate private key
        subprocess.call(['openssl', 'genpkey', '-algorithm', 'RSA', '-out', 'private.key'])
    
        # Generate CSR
        subprocess.call(['openssl', 'req', '-new', '-key', 'private.key', '-out', 'csr.csr', '-subj',
                         '/CN={}/O={}/OU={}/L={}/ST={}/C={}'.format(signer_common_name, signer_organization,
                                                                    signer_organization_unit, signer_locality,
                                                                    signer_state, signer_country)])
    
        # Generate self-signed certificate
        subprocess.call(['openssl', 'x509', '-req', '-days', '365', '-in', 'csr.csr', '-signkey', 'private.key',
                         '-out', 'certificate.crt'])
    
        return jsonify({'message': 'Certificate generated successfully.'}), 200
    
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    

    @app.route('/sign_pdf', methods=['POST']) def sign_pdf(): try: # Get the uploaded PDF file pdf_file = request.files['file'] pdf_data = pdf_file.read()

        # Load the PDF
        pdf = PdfReader(pdf_data)
    
        # Sign the PDF
        signature_image = 'signature.png'  # Path to the signature image
        output_pdf = 'signed_document.pdf'  # Path to the signed PDF
    
        for page in pdf.pages:
            page.merge_page(page)
    
        # Add the signature image to the first page
        signature_page = pdf.pages[0]
        signature_page.add_image(signature_image, x=100, y=100, width=200, height=50)
    
        # Save the signed PDF
        pdf_writer = PdfWriter()
        for page in pdf.pages:
            pdf_writer.add_page(page)
    
        with open(output_pdf, 'wb') as output_file:
            pdf_writer.write(output_file)
    
        # Remove the temporary files
        os.remove('private.key')
        os.remove('csr.csr')
        os.remove('certificate.crt')
    
        return jsonify({'message': 'PDF signed successfully.'}), 200
    
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    

    if name == 'main': app.run(debug=True)

    9 - Highest
    State of Progress
    2023-06-30
    Empty
    2023-07-07
    Alok Ranjan (alok), Saurabh Kumar Pandey (saurabh), Abhijeet Anand (abhi912), Dhatri Pathak (dhatri)
    Saurabh Kumar Pandey (saurabh)
    2023-07-08
    In Progress
    Attachments
    Empty
    References
    References list is empty