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

Community
  • 1
  • 1
Veer Singh
  • 913
  • 2
  • 11
  • 26
  • You can't just redirect some output to a PDF file and expect it to be a valid PDF. PDF has a specific format. – ctc Oct 31 '15 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 – Veer Singh Oct 31 '15 at 18:10

1 Answers1

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
ctc
  • 2,640
  • 1
  • 16
  • 18
  • 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. – ctc Oct 31 '15 at 18:13
  • I have both OSX and Windows 7 64-bit – Veer Singh Oct 31 '15 at 18:14
  • 1
    I have edited the answer to show you how you would approach the problem on OS X. – ctc Oct 31 '15 at 18:39
  • Thanks! One more thing (sorry, very unfamiliar with UNIX paths) If my repo is under my 'Downloads' in a folder called 'Lua-Research', what would my REPOSITORY path be? – Veer Singh Oct 31 '15 at 18:47
  • 1
    Most likely `~/Downloads/Lua-Research`. To be sure, you can find out using Finder: http://lifehacker.com/5102717/show-a-folders-full-path-in-finders-title-bar – ctc Oct 31 '15 at 18:49
  • 1
    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. – ctc Oct 31 '15 at 18:53
  • i tried the bash code above, but it didnt iterate all files. but the line `<(find $REPOSITORY -type f -iname '*.lua')` will always produce `/dev/fd/63`. what is this `<(...)` command? – Hadi KAR Sep 29 '22 at 06:05
  • it was [process substitution](https://wiki.bash-hackers.org/syntax/expansion/proc_subst). what happened was there is mistake in my find command. i wrote '-iname '*.(ts|tsx)''. weirdly it cannot parse regex – Hadi KAR Sep 29 '22 at 07:01
  • Correct -- `find` doesn't support regular expressions with the `-iname` parameter. There are [other parameters](https://stackoverflow.com/questions/6844785/how-to-use-regex-with-find-command), however, that allow you to specify a regular expression. – ctc Sep 29 '22 at 20:54