5

I am trying to take all files from a specific repository I have on GitHub and turn them into PDF's. I have read and seen an example here

However, I am still a bit confused on how to go about doing this. I have to take all my files and turn them into a single PDF file to upload it to a site for college.

I am not extremely familiar with UNIX commands and I was trying to do the following:

for i in *.lua; do echo "$i"; echo "---"; cat "$i"; echo ; done > result.pdf

Then I was going to take all the pdfs and merge them together, but I was hoping there was a better way to go about doing this. I'm specifically dealing with only .lua and .md file-extensions.

I personally believe this can be done with the use of some UNIX commands, but as mentioned, I am not familiar with them.

To summarize, my main objective is to take a series of files located on a repository on Github and merge them into one PDF file. Even obtaining multiple PDF files is ok is that is the best that can be done. Even a .word file format is good enough.

Operating System: OSX or Windows 7 64-bit

2
  • You can't just redirect some output to a PDF file and expect it to be a valid PDF. PDF has a specific format. Commented Oct 31, 2015 at 18:09
  • @ctc Thanks for the response! What would be your suggestion on going about doing this? I'm fine even with a .word format Commented Oct 31, 2015 at 18:10

2 Answers 2

9

I suggest looking into a syntax highlighter. Pygments has the ability to generate HTML, RTF, etc. If you chose to output HTML files, you could then use a tool like wkhtmltopdf to convert the syntax-highlighted HTML files to PDF files.

Alternatively, Pygments can do LaTeX. If you're familiar with LaTeX, then you can have Pygments generate LaTeX output, and use pdflatex to generate your PDF files.

You said you're on OS X. To install Pygments, open a terminal and type:

sudo easy_install Pygments

This will install a program pygmentize that you can use to transform code.

Next, install wkhtmltopdf.

Now, you can take a file, syntax highlight it, and convert it to PDF:

pygmentize -l ruby -f html -O full,style=vim test.rb > test.html
wkhtmltopdf test.html test.pdf

Here, I show the conversion of a Ruby script. Of course, you'd want to use -l lua if you're converting Lua scripts.

You can then incorporate these commands into a shell script that traverses over a directory recursively, e.g.

#!/bin/bash

# Change this to the repository directory
REPOSITORY=/path/to/the/repo

# Iterate over the repository
while read source_file
do
  filename=$(basename $source_file)
  dir=$(dirname $source_file)

  # For each .lua file found, generate an HTML file in /tmp
  pygmentize -l lua -f html -O full,style=vim $source_file > /tmp/${filename}.html

  # Convert the HTML file to a PDF file in the same directory 
  # as the .lua file
  wkhtmltopdf /tmp/${filename}.html ${dir}/${filename}.pdf

done < <(find $REPOSITORY -type f -iname '*.lua')

Put this in a file named convert.sh. Then, to run it, type:

chmod +x convert.sh
./convert.sh
Sign up to request clarification or add additional context in comments.

9 Comments

You haven't specified what operating system you're using. This would be helpful to know in order to suggest the appropriate commands to use to install this software.
I have both OSX and Windows 7 64-bit
I have edited the answer to show you how you would approach the problem on OS X.
Most likely ~/Downloads/Lua-Research. To be sure, you can find out using Finder: lifehacker.com/5102717/…
You can also, of course, try typing cd ~/Downloads/Lua-Research. If that works and contains the files you expect (type ls), then you know you got the directory right.
|
0

Here is another way. I created Python code to generate a PDF file with syntax-highlighted source codes from a given folder.

python3 print_code.py --source_folder ~/Documents/model_code --output_folder ~/Documents --file_extension 'java' --syntax_language 'java'

Check the requirements to run the code at https://github.com/mohitWrangler/source-code-to-pdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.