Didit Console

Generate PDF

The Generate PDF API allows you to generate a PDF report for a verification session.

The response is a binary PDF file containing detailed information about the verification session. Below you can find an example on how to save a generated PDF:

import requests
import os
 
def generate_session_pdf(session_id):
    """
    Generate a PDF report for a verification session and save it to file.
 
    Args:
        session_id (str): The ID of the verification session.
 
    Returns:
        str: The path to the saved PDF file or None if an error occurred.
    """
    url = f"https://verification.didit.me/v1/session/{session_id}/generate-pdf/"
    headers = {
        "x-api-key": f"YOUR_API_KEY"
    }
 
    try:
        response = requests.get(url, headers=headers, stream=True)
        response.raise_for_status()
 
        # Get the filename from the Content-Disposition header or use a default
        filename = f"session_{session_id}.pdf"
        content_disposition = response.headers.get('Content-Disposition')
        if content_disposition and 'filename=' in content_disposition:
            filename = content_disposition.split('filename=')[1].strip('"')
 
        # Save the PDF to a file
        file_path = os.path.join(os.getcwd(), filename)
        with open(file_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
 
        print(f"PDF saved to {file_path}")
        return file_path
 
    except requests.RequestException as e:
        print(f"Error generating PDF: {e}")
        # Try to get the error message from the response
        try:
            error_detail = e.response.json().get('detail', str(e))
            print(f"Server returned: {error_detail}")
        except:
            pass
        return None
const generateSessionPDF = async (sessionId) => {
  const endpoint = `https://verification.didit.me/v1/session/${sessionId}/generate-pdf/`;
  const token = await getClientToken();
 
  if (!token) {
    console.error('Error fetching client token');
    throw new Error('Authentication failed');
  }
 
  const headers = {
    Authorization: `Bearer ${token.access_token}`,
  };
 
  try {
    const response = await fetch(endpoint, {
      method: 'GET',
      headers,
    });
 
    if (!response.ok) {
      const errorData = await response.json();
      console.error('Error generating PDF:', errorData.detail || errorData.message);
      throw new Error(errorData.detail || 'Failed to generate PDF');
    }
 
    // Convert the response to a blob
    const blob = await response.blob();
 
    // Create a download link
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `session_${sessionId}.pdf`;
    document.body.appendChild(a);
    a.click();
 
    // Cleanup
    window.URL.revokeObjectURL(url);
    document.body.removeChild(a);
 
    return true;
  } catch (err) {
    console.error('Error:', err);
    throw err;
  }
};
Language
Credentials
Header
Click Try It! to start a request and see the response here!