This post is to create a tool that allow the users to create custom PDF files from source code in Linux
1) First we install the needed applications. We need to install highlight and wkhtmltopdf. To install highlight we run the command in the terminal
sudo apt install highlight
To install wkhtmltopdf however we need to download the binaries from the development repositories due to the distributions almost always have an incomplete version lacking of Qt functionality that is needed for proper function of wkhtmltopdf. So we go to the wkhtmltopdf github repositories
https://github.com/wkhtmltopdf/packaging/releases and download the needed binary. We install it and then
2) Create a bash scrip with the following code and saved with the name cpp2pdf.sh
#!/bin/bash
INPUT="$1"
USERNAME="$2"
FILENAME=$(basename "${INPUT%.*}")
DATE=$(date +"%Y-%m-%d %H:%M:%S")
PDF_OUTPUT_DIR="$HOME/EXPORTED_PDF"
# Step 1: Generate HTML with syntax highlighting
highlight -O html \
--font=Courier \
--font-size=17 \
--style=github \
--line-length=96 \
--line-numbers \
--zeroes \
--reformat=allman \
--input="$INPUT" \
--output="$PDF_OUTPUT_DIR/$FILENAME.html"
# Step 2: Convert to PDF
wkhtmltopdf --enable-local-file-access \
--header-html "$PDF_OUTPUT_DIR/header.html" \
--header-left "File: $FILENAME" \
--header-right "Author: $USERNAME" \
--footer-center "Page [page] of [topage] | $DATE" \
--page-size A4 \
--orientation Portrait \
--margin-top 30mm \
--margin-bottom 20mm \
--margin-left 10mm \
--margin-right 10mm \
"$PDF_OUTPUT_DIR/$FILENAME.html" \
"$PDF_OUTPUT_DIR/$FILENAME.pdf"
echo "Created: $PDF_OUTPUT_DIR/$FILENAME.pdf"
echo " File: $FILENAME"
echo " User: $USERNAME"
echo " Date: $DATE"
# Open the PDF after creation
if [ -f "$PDF_OUTPUT_DIR/$FILENAME.pdf" ]; then
xdg-open "$PDF_OUTPUT_DIR/$FILENAME.pdf" &
exit 0
else
echo "Error: PDF was not created"
exit 1
fi
3) We give execution permission to the scrip with the command
4) In our Home directory we create a directory with the name EXPORTED_PDF (see that this name appears in the script and is the place where the PDF files are placed). You can change this directory but you have to change the script too.
5) In the EXPORTED_PDF directory we create a custom HTML file named header.html that contains a custom header
6) Now in Code::Blocks we add an external tool going to the Tools menu then to Configure Tools and we click Add to add the new tool
7) In the name field put: Export clean PDF
8 ) In executable browse to the script file cpp2pdf.sh
9) In working directory put (please note the quotation marks that are needed)
10) In Parameters put (change the "Author name" to your name or organization name), please note the double quotation marks
"$(ACTIVE_EDITOR_FILENAME)" "Author name"
11) Finally we click Ok and the tool is ready
Now we can simply open some source code file and go to Tools -> Export clean PDF and the current file shown in editor is exported to PDF in the EXPORTED_PDF directory.
This simply tools highlight and wkhtmltopdf can be customized to create great quality outputs. Note that there are many styles that can be used in highlight.
Is important to remember to create the header.html file in the EXPORTED_PDF directory for add custom header, you can remove the custom header by removing the following line of the script
--header-html "$PDF_OUTPUT_DIR/header.html" \
You can also add a custom footer by adding the following line just below the "--header-html "$PDF_OUTPUT_DIR/header.html" \" part of the code
--footer-html "$PDF_OUTPUT_DIR/footer.html" \
The custom headers and footers can be customized to include custom fonts, custom formatting, text and so on. See at the attached file to see the results. There are dark themes in highlight too.
The following is an example of a custom header (note that must be a directory inside the EXPORTED_PDF directory named "fonts" containing the custom font Titillium Web)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/index-style.css" type="text/css"/>
<style>
@font-face{
font-family: 'Titillium Web';
src: url('/fonts/TitilliumWeb-Regular.ttf');
src: local('Titillium Web'),
url('/fonts/TitilliumWeb-Regular.ttf') format('truetype');
}
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
font-size: 10px;
color: #555;
}
.header {
width: 100%;
text-align: center;
border-bottom: 1px solid #ccc;
padding-bottom: 2px;
}
</style>
</head>
<body>
<div class="header">
<span style="font-family: 'Titillium Web';font-size: 2.5em">My Company</span>
</div>
</body>
</html>
You can add more wild customization if you wish.