Code2PDF 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global Code2PDF --version 1.1.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Code2PDF --version 1.1.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Code2PDF&version=1.1.0
                    
nuke :add-package Code2PDF --version 1.1.0
                    

Code2PDF

This simple(ish) program takes in a directory containing all of your source code and returns a .pdf file containing nicely formatted. It does this by launching a Firefox instance and manually injecting HTML containing each <code> block, then running highlight JS on the page. Then it runs the print() JavaScript function to save it as a .pdf

Usage

This program requires you to have installed .NET Core 8, and works on Linux. It should, in theory, work on Windows and Mac but I haven't tested it as of now.

Install

For personal use, run dotnet tool install -g code2pdf in favourite terminal (if your using Windows, I think this will only work in PowerShell), then follow the command line instructions

Development

This program requires you to have installed .NET Core 8. Once installed, follow the next steps

git clone https://gitlab.com/jedsaxon/code2pdf
cd code2pdf
dotnet build

Command Line

--src,     -s  |  Source Code Directory. It will recursively include all files within it
--out,     -o  |  Output PDF File Path. Once the document has been generated, it will save it as a .pdf file to this path
--help,    -h  |  Shows this page
--ff-log,  -fl |  Sets what kind of logs Firefox should output in the console. Options are (case-sensitive) Trace, Debug, Config, Info, Warn, Error, and Fatal. Default is 'Fatal'."
--template -t  |  Select your own custom template file for code to be displayed on. See https://gitlab.com/jedsaxon/code2pdf for details about using custom templates

Examples

The following command will take the code from ~/my-project, and output a .pdf file in ~/my-fancy-code.pdf

code2pdf --src ~/my-project --out ~/my-fancy-code.pdf

Templates

The included template file is included in /ConsoleApplication/Resources/template.html and will be used if the --template -t arguments are not passed in. If you would like to use a custom template (perhaps with custom theme), you must include the following:

  1. Include hljs, hljs-line-numbers:
<head>
    
    <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css'>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js'></script>
    <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/styles/github.min.css'>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js'></script>
    <script src='https://cdn.jsdelivr.net/npm/highlightjs-line-numbers.js@2.8.0/dist/highlightjs-line-numbers.min.js'></script>
    
</head>
  1. Add an element with a the id code, preferably containing nothing inside. Your code will be inserted into there
<section id='code'></section>
  1. Add some custom styling (optional)
<style>
    .code-box {
        /* Container for <pre> */
    }

    pre {
        /* Container for <code> */
    }

    code {
        /* Actual Text */
    }

    .hljs-ln-numbers {
        /* Line Numbers */
    }

    .hljs-ln-code {
        /* Highlighted Code Elements */
    }
</style>

Extra

If you don't want the browser window to pop up, when running the script, set the MOZ_HEADLESS environment variable to one. Probably should NOT set that globally. If you are using Linux (and probably macOS), then you can use the command export MOZ_HEADLESS=1 to do this, then run dotnet run. If you are using Windows, it would probably take you less time to install an actually usable operating system than to figure out how to set an environment variable.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.2.1 210 1/2/2025
1.2.0 184 7/25/2024
1.1.0 170 7/22/2024
1.0.3 174 7/21/2024
1.0.2 173 7/21/2024
1.0.1 143 7/21/2024
1.0.1-pre 139 7/21/2024