Skip to content


Subversion checkout URL

You can clone with
Download ZIP


PDF encoding support #24

metade opened this Issue · 5 comments

4 participants



It would be great to use Coderay to generate PDFs using something like Prawn - has anyone looked to develop this?

I've had a go myself but I don't think my approach is very good as I don't fully understand the Coderay internals:

The issues I have with my approach are that:

  • I'm defining styles in the encoder using a hash, yet the HTML encoder seems to fetch data from a CSS
  • It's really reliant on Prawn inline formatting, which might not be so bad for doing snippets (e.g. like the div encoder) but it would be good to be able to generate complete pdf files too
  • I'm not sure the best way to unit test the encoder

It would be great to get some feedback on my approach and some pointers on how it would be best to proceed!



Hi Patrick,

You might want to try a tool like PDFkit that will convert HTML into a PDF. I'm not sure how well it will carry over the styling, but it's worth a shot.




Sorry if this is a bit of strange request! :)

I'm working on a side project where I want to format PDFs with large chunks of source code (thus why I'm interested in using Coderay to do syntax highlighting) but also bits of text, table of contents, index etc - potentially so it could be printed as a book.

So this is why I'm hooking up Coderay to Prawn - I don't think that PDFkit will give me enough flexibility to format a number of source code files into a single PDF with the control I require.

I accept that it's probably such an odd feature that it might not be appropriate to sit within the Coderay gem - I could create a new gem (e.g. coderay-prawn) for it?

I guess what I'm after is a bit of advice on how to use the Coderay Styles system in my PDF encoder - I haven't really been able to figure out how it works and how it's used with the other encoders (e.g. the html one).



I could create a new gem (e.g. coderay-prawn) for it?

Definitely the best idea.

I haven't really been able to figure out how it works and how it's used with the other encoders (e.g. the html one).

It's not simple, that's true.

Basically, Scanners call these methods on the given encoder object:

  • text_token with text and token kind (a symbol)
  • begin_group and end_group with a token kind; this is for nested tokens, like Strings that contain other tokens
  • begin_line and end_line with a token kind; this is mainly for diffs where you have lines that need a background color

Your encoder only needs to react to these calls correctly. Take a look at for a simple example.


@metade have you seen this

Are you still interested to work on it? I can help. I need it for one project.


I'll close this issue, but if you need my help for creating a separate gem for PDF output, let me know or ask here :-)

@korny korny closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.