Skip to main content

Command Palette

Search for a command to run...

Convert CPP to PDF with Syntax Highlighting

Published
1 min read
Convert CPP to PDF with Syntax Highlighting
M

DevOps Engineer | Kubernetes | Python | Terraform | AWS | GCP

I just had converted a bunch of CPP files to PDF with Syntax Highlighting. I think it will be helpful to share how I did that.

Required Packages

  • enscript
  • ps2pdf

Use package manager to download them.

Now modify this script to your own configs.

#!/bin/bash

TARGET='/home/minhaz/cppfiles'

cd $TARGET

find . -type f -name '*.cpp' | while read CPPFILE
do
    TITLE=$(basename $CPPFILE .cpp)
    echo $CPPFILE | xargs enscript --color=1 -C -Ecpp -B -t $TITLE -o - | ps2pdf - $TITLE.pdf
done

Run the script and you'll find PDF files generated with the name of each CPP file.

Here is a preview of one CPP file I had.

CPP to PDF

Cool, eh?

More from this blog

M

Minhaz's Blog

111 posts