Is there a command line C++ to PDF converter with syntax highlighting? [closed]

别来无恙 提交于 2019-11-29 01:04:00

问题


I need to supply "Source code documents w/ Line numbers" which is essentially just a PDF of the source code with syntax highlighting and Line numbers. Is there any existing command line tools for windows that I could call from a script as a "build release version" script?

Right now I'm doing it manually using VC++, which isn't even the dev enviroment the code is for a TI processor, and a PDF printer driver, which has a pop up for each file I print.


回答1:


Two syntax highlighters I use are enscript and source-highlight.

The first can output to PostScript (that you can convert to PDF using ps2pdf), the second produces output in HTML, LaTeX and other formats.

Both should be available via Cygwin

EDIT: On my system the following command will print all the cpp files in the current subtree.

find . -name "*.cpp" | xargs enscript -Ecpp -fCourier8 

While the following will produce a code.pdf file with the same content

find . -name "*.cpp" | xargs enscript -Ecpp -fCourier8 -o - | ps2pdf - code.pdf

PS: and give the --color=1 for color output and -C for line numbers.

find . -name "*.cpp" | xargs enscript --color=1 -C -Ecpp -fCourier8 -o - | ps2pdf - code.pdf



回答2:


I use this. It generates .ps. Then you can run ps2pdf.

# Copyright 2004 Rutger E.W. van Beusekom.
# Distributed under the Boost Software License, Version 1.0. (See
# accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

outfile=$1
shift

a2ps -1 --highlight=normal --pretty-print=cxx --line-numbers=1 -M a4 -L80 -g \
-o $outfile $* --prologue=color --right-title=%p. --left-title \
--left-footer --right-footer --header --medium=a4

You could also use Doxygen with source browsing enabled. There is also htmlize.el by Hrvoje Niksic for emacs.




回答3:


I tested several alternatives and enscript had the best result to print the source code. So, I wrote this bash script (needs bash, enscript, ps2pdf, pdfjam) a few minutes ago to collect all sources in the current directory:

usage example: ./script.sh *.h *.c

#!/bin/bash
test "x$1" == "x" && echo "usage: $0 <sourcefiles>" && exit 0

for f in "$@" ; do enscript $f -o $f.ps -E -C ; done

for f in *.ps ; do echo ps2pdf $f ; ps2pdf $f && rm $f ; done

rm OUTPUT.pdf 2>/dev/null
pdfjam *.pdf && mv *pdfjam.pdf OUTPUT.pdf

echo
echo DONE:
echo OUTPUT.pdf

WARNING: This script is a bad hack and will delete *.ps and OUTPUT.pdf in the current directory. It assumes all input files are in the current directory.



来源:https://stackoverflow.com/questions/2649373/is-there-a-command-line-c-to-pdf-converter-with-syntax-highlighting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!