Giter Club home page Giter Club logo

rails-latex's Introduction

<img src=“https://travis-ci.com/amagical-net/rails-latex.svg?branch=master” alt=“Build Status” />

Rails-LaTeX

  • Git: github.com/baierjan/rails-latex

  • Original author: Geoff Jacobsen

  • Copyright: 2010-2015 Geoff Jacobsen, Jan Baier and contributors

  • License: MIT-LICENSE

Description

rails-latex is a renderer for rails, which allows tex files with erb to be turned into an inline pdf.

Synopsis

app/helpers/application_helper.rb:

def lesc(text)
  LatexToPdf.escape_latex(text)
end

app/views/stories/show.html.erb:

...
<%= link_to "print", story_path(@story,:format => :pdf) %>

app/views/stories/show.pdf.erb:

...
<%= lesc @story.name % >

app/views/layouts/application.pdf.erbtex:

\documentclass[12pt,a4paper,sloppy,< %= @landscape % >]{article}
...
< %= yield % >
\end{document}

config/initializers/mime_types.rb:

...
Mime::Type.register "application/pdf", :pdf, ['text/pdf'], ['pdf']

Only the file containing the \documentclass should be of type: .pdf.erbtex . Partials and views (when there is a layout) should be of type .pdf.erb .

If a LaTeX package requires more than one parser run add the following to the .pdf.erbtex file:

<% @latex_config={:parse_runs => 3} %>

You can override the defaults like so:

LatexToPdf.config.merge! :command => 'xetex', :arguments => ['-etex'], :parse_runs => 2

or to change just the arguments:

LatexToPdf.config[:arguments].delete('-halt-on-error')

The defaults are:

command: 'pdflatex'
arguments: ['-halt-on-error']
parse_runs: 1

The last log file is moved to tmp/rails-latex/input.log , and the corresponding source TeX file to tmp/rails-latex/input.tex . If the PDF is not produced the build directory is not removed; an archive script should be written to occasionally clean up the tmp/rails-latex directory.

See the rails application under examples/rails-latex-demo/ for a working example.

Generating a String

If you need to generate the LaTeX document as a string you can use the render_to_string method. You will probably need to pass the :layout => true option to invoke the PDF generator. Here is an example to generate an email attachment:

In the controller you will have something along the lines of:

def send_email_method
  output = render_to_string(:template => "/your_model/your_view.pdf.erb", :layout => true)
  MyMailer.email_sender(current_user, output).deliver
end

The thing to note is the :layout => true which is needed so that the pdf template can grab the application erbtex layout. Then in your mailer:

def email_sender(user, pdf_attachment)
  @user = user
  attachments["attachment_name.pdf"] = {:mime_type => 'application/pdf', :content => pdf_attachment}
  mail( .... )
end

Exception handling and debugging

When LaTeX render fails, RailsLatex::ProcessingError is thrown. In your code, you can catch it and react according to your needs. To make your life easier, TeX source and log are attached to the exception as instance variables src and log.

Requirements

  • ruby 1.8 or 1.9

  • rails 3 or later

Install

  • gem install rails-latex

Development

Developing rails-latex requires bundler and RedCloth

  • rake test

  • rake build

Contributions

  • Geoff Jacobsen

  • Tommaso Patrizi

  • Klaus Reske

  • Jan Baier

  • Jakub Cerny

rails-latex's People

Contributors

baierjan avatar jacott avatar cernyjakub avatar macite avatar master-lincoln avatar bryanyee avatar danfinnie avatar allavena avatar anthonyjsmith avatar burisu avatar danniyu avatar hperl avatar hammady avatar mkamensky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.