Email to PDF Converter

repository·main·Indexed 18 days ago

https://github.com/nickrussler/email-to-pdf-converter

A tool for converting .eml and .msg email files into PDF documents. It functions as a command-line tool, a GUI desktop application, or an integrated library. The converter parses MIME structures, converts them to HTML, and uses wkhtmltopdf for final PDF rendering. Key features include attachment extraction, proxy configuration, and customizable page sizes and DPI settings.

Tokens
2.9K
Snippets
6
Records
8
Agent score
13%

What's inside email-to-pdf-converter

  1. How Email to PDF Converter works

    main

    The converter transforms email files (.eml or .msg) into PDF documents using a three-step process:

    1. Parsing: It parses and cleans the MIME structure.
    2. HTML Conversion: It converts the parsed structure into HTML.
    3. PDF Generation: It uses wkhtmltopdf to render the generated HTML into a final PDF file.

    Key features include handling inline images, managing corrupt MIME headers, and supporting proxy configurations.

  2. Build the project from source

    main

    To build the project, clone the repository (do not use a ZIP download as the .git folder is required). Use the following Gradle commands:

    • Create a self-contained JAR: gradlew shadowJar (outputs to build/libs).
    • Create Windows launchers: gradlew dist (creates JAR and Windows EXE launchers in build/libs). Requires Launch4j in PATH.
    • Create Windows installer: gradlew innosetup (outputs to build/innosetup). Requires Launch4j and Inno Setup issc.exe in PATH.
    • Run tests: gradlew check (executes unit tests and generates reports).
    # Create a single self contained Jar
    ./gradlew shadowJar
    
    # Create Windows exe launchers (requires Launch4j)
    ./gradlew dist
    
    # Create Windows setup (requires Launch4j and Inno Setup)
    ./gradlew innosetup
    
    # Execute unit tests
    ./gradlew check
  3. Install and set up Email to PDF Converter

    main

    To use the converter, ensure you meet the following prerequisites:

    1. Java Runtime: A complete JRE is required (not a headless version). For example, on Debian, install openjdk-11-jre.
    2. wkhtmltopdf: The wkhtmltopdf binary must be installed and available in your system $PATH.

    Download

    You can download the latest binaries from the official releases page. Available formats include:

    • Windows setup.exe
    • JAR file (suitable for non-Windows OS, provided wkhtmltopdf is in the PATH).
  4. Configure date formatting via VM arguments

    main

    Dates in the generated PDF are formatted using the default locale. You can override this by passing standard Java VM arguments when running the application.

    Example: Change language/locale

    java -Duser.language=en-US -jar emailconverter.jar example.eml

    Example: Change timezone

    java -Duser.timezone="Asia/Kolkata" -jar emailconverter.jar example.eml
    java -Duser.language=en-US -jar emailconverter.jar example.eml
  5. Reference: EmailToPDFConverter CLI options

    main

    Full list of available command-line options for EmailToPDFConverter.

    Usage: EmailToPDFConverter [options] <email-file>
      Options:
        -d, --debug
          Debug mode
          Default: false
        -e, --error
          Display only Error messages.
          Default: false
        -aa, --add-attachment-names
          Add the list of attachment names under the body text
          Default: false
        -a, --extract-attachments
          Extract Attachments.
          Default: false
        -ad, --extract-attachments-directory
          Extract Attachments to this Directory, if this option is not present the
          directory is besides the pdf as "<pdf-name>-attachments".
        -?, --help
          Print this help.
        -hh, --hide-headers
          Do not add email headers (subject, from, etc.) at the beginning of the
          PDF document.
          Default: false
        -o, --output-filepath
          Filepath of the produced PDF document. If this option is ommited the PDF
          will be placed alongside the email File.
        -s, --page-size
          Set wkhtmltopdf paper size to: A4, Letter, etc. (default A4)
          Default: A4
        -r, --dpi
          Set wkhtmltopdf DPI. (default 300)
          Default: 300
        -p, --proxy
          Proxy (e.g. "http://10.64.1.74:81"). If "auto" is supplied the default
          system proxy will be used.
        -q, --quiet
          Do not display any messages at all.
          Default: false
        -gui, --show-graphical-user-interface
          Show graphical user interface (other parameters are ignored when using
          this switch).
          Default: false
        -v, --version
          Print the version number.
          Default: false
  6. Use the EmailToPDFConverter CLI

    main

    The command-line interface allows you to convert email files with various customization options.

    Basic Usage:

    java -jar emailconverter-2.1.1-all.jar example.eml

    (Note: Ensure wkhtmltopdf is in your PATH)

    Common Options:

    • -o, --output-filepath <path>: Specify where the PDF is saved. If omitted, it is placed alongside the source email.
    • -a, --extract-attachments: Extract attachments from the email.
    • -ad, --extract-attachments-directory <dir>: Specify a directory for extracted attachments. If not provided, it defaults to <pdf-name>-attachments next to the PDF.
    • -hh, --hide-headers: Prevents email headers (Subject, From, etc.) from being added to the top of the PDF.
    • -s, --page-size <size>: Set paper size (e.g., A4, Letter). Default is A4.
    • -r, --dpi <number>: Set the DPI for wkhtmltopdf. Default is 300.
    • -p, --proxy <url>: Use a proxy (e.g., http://10.64.1.74:81). Use auto for the system proxy.
    java -jar emailconverter-2.1.1-all.jar example.eml
  7. Reference: CLI flags and options

    main

    The following table lists all available command-line flags and options for the Email to PDF Converter.

    -o, --output-filepath <path>      Filepath of the produced PDF document. If this option is ommited the PDF will be placed alongside the email File.
    -p, --proxy <proxy>               Proxy (e.g. "http://10.64.1.74:81"). If "auto" is supplied the default system proxy will be used.
    -d, --debug                        Debug mode
    -e, --error                        Display only Error messages.
    -q, --quiet                        Do not display any messages at all.
    -?, --help                         Print this help.
    -v, --version                      Print the version number.
    -hh, --hide-headers                Do not add email headers (subject, from, etc.) at the beginning of the PDF document.
    -aa, --add-attachment-names        Add the list of attachment names under the body text
    -a, --extract-attachments          Extract Attachments.
    -ad, --extract-attachments-directory Extract Attachments to this Directory, if this option is not present the directory is besides the pdf as "<pdf-name>-attachments".
    -gui, --show-graphical-user-interface Show graphical user interface (other parameters are ignored when using this switch).
    -s, --page-size <size>             Set wkhtmltopdf paper size to: A4, Letter, etc. (default A4)
    -r, --dpi <dpi>                    Set wkhtmltopdf DPI. (default 300)
    <email-file>                       The path to the email file(s) to convert.
  8. Command line interface options for Email to PDF Converter

    main

    The Email to PDF Converter CLI accepts several options to control output destination, attachment handling, PDF rendering quality, and logging behavior.

    Usage Pattern

    email-to-pdf-converter [options] <email-file> [email-file ...]

    Output and File Handling

    • -o, --output-filepath <path>: Specify the filepath for the produced PDF. If omitted, the PDF is placed alongside the source email file.
    • <email-file>: One or more paths to the email files you wish to convert.

    Attachment Management

    • -a, --extract-attachments: Extract attachments from the email.
    • -ad, --extract-attachments-directory <dir>: Specify a directory to extract attachments to. If not provided, attachments are saved to a directory named <pdf-name>-attachments next to the PDF.
    • -aa, --add-attachment-names: Add a list of attachment names under the body text in the PDF.

    PDF Rendering and Layout

    • -s, --page-size <size>: Set the paper size (e.g., A4, Letter). Default is A4.
    • -r, --dpi <dpi>: Set the DPI for rendering. Default is 300.
    • -hh, --hide-headers: Do not include email headers (Subject, From, etc.) at the beginning of the PDF document.

    Network and Proxy

    • -p, --proxy <url>: Specify a proxy (e.g., http://10.64.1.74:81). Use auto to use the default system proxy.

    Logging and UI

    • -d, --debug: Enable debug mode.
    • -e, --error: Display only error messages.
    • -q, --quiet: Suppress all messages.
    • -gui, --show-graphical-user-interface: Launch the graphical user interface. Note that all other parameters are ignored when using this flag.
    • -?, --help: Print the help documentation.
    • -v, --version: Print the version number.
    # Example: Convert an email, extract attachments to a specific folder, and hide headers
    email-to-pdf-converter -a -ad ./my-attachments -hh input.eml