GithubHelp home page GithubHelp logo

amagical-net / rails-latex Goto Github PK

View Code? Open in Web Editor NEW
141.0 141.0 48.0 156 KB

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

License: MIT License

Ruby 95.08% TeX 4.92%

rails-latex's People

Contributors

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-latex's Issues

Undefined control sequence textbacklash

I think this is because Latex2Pdf.escape_latex is emitting \textbackslash{} where the {} is not appropriate. That brace pair is needed for characters like tilde because the tilde needs a blank character to be drawn over.

Doesn’t work in Ruby 1.8

rails-latex/lib/latex_to_pdf.rb, line 13: You cannot pass a hash to Kernel.system in Ruby 1.8, only in Ruby 1.9.

Quick and dirty, I removed the options. Maybe there is a more sensible solution.

Images

Can you please add a simple example or update the nice one you provided, on how to add some image? I'm trying right now, but i'm unable to see the image.

Render_to_string pdf corrupted

Hi. I use your gem. It works perfect, but I have some issues with render to string.

usage of:

  • render_to_string : render_to_string(:action =>'action_file.rb, :layout => "action_template")

It is working correctly but when I exceed some amount of text in view (latex source), the pdf is corrupted. Less text -> pdf ok. So I tried to use normal render by using large size of file and here it is correct. So problem is with proccessing it via render_to_string. I have no idea what is wrong. Have you ever met with this problem? Please give me some idea to make it working. Thanks a lot

Some details:
I'am using some parts and inserting them by yield. It is generating latex source dynamically.

error generating pdf

I get this error in input.log :

can't change access mode from "rb+" to "a":
/home/tim/.rvm/gems/ruby-1.9.2-p320/gems/rails-latex-1.0.11/lib/rails-latex/latex_to_pdf.rb:25:in `reopen'

I fixed it by commenting lines 25 and 26 in latex_to_pdf.rb :

      #STDOUT.reopen("input.log","a")
      #STDERR.reopen(STDOUT)

Any idea where this comes from ? All I can see is that tmp/rails-latex/input.log is missing, only tmp/rails-latex/[pid]-[hash]/input.log is generated.

Thanks for your help,

Jean

can't convert Hash into String

I receive this error trying to typeset my show view.

this is my application.pdf.erbtex
\documentclass[12pt,a4paper,sloppy]{article}
<% @latex_parse_twice=true %>
\usepackage{lastpage}
\begin{document}

<%= yield %>

\end{document}

Rails said the error is located on the line with "\end{document}".
Any clue? (rails 3.0.3, rails-latex 1.0.1)
However the rails example you provide on github works well.

RailsLatex::ProcessingError is not thrown when using layout

I am building a PDF using a structure similar to your README example with alayouts/application.pdf.erbtex with a yield and a view template show.pdf.erb which contains the \begin{document} ... \end{document} section.

I am attempting to rescue exceptions in the controller. When an processing error occurrs, the exception that is thrown is an ActionView::Template::Error. How do we get the RailsLatex::ProcessingError exception?

Interestingly, the development view (in red) shows that an RailsLatex::ProcessingError occurs, but the rescued exception comes back as ActionView::Template::Error. The error message is the one generated by latex_to_pdf.rb "rails-latex failed: See ... for details"

#generate_pdf may return an incomplete pdf

There are cases in which an incomplete file may be saved and the method will return cleanly, with no indication that an error occurred and that the pdf is incomplete.

For example, if using xelatex, an error after the first page may cause an incomplete file to be written to the tmp directory and returned. (In the case that I tested, this was not an issue when using pdflatex).

LatexToPdf#generate_pdf only raises an exception if a file doesn't exist at the specified tmp location ( <root>/tmp/rails-latex/<Process.pid>-<Thread.current.hash>/input.pdf ) after running the tex commands. An incomplete pdf file will return without an error. It seems that when using xelatex, the -halt-on-error option isn't enough to stop invalid pdf generation. And even with pdflatex, a more robust error handler may be more desirable than just checking for the existence of a file.

One solution is to validate that the exit status of the forked child process is zero (indicating that the tex commands did not raise an exception within the child process) before returning a result. If the exit status is nonzero, then raise an exception.

$?.exitstatus.zero?

Or, if the "English" library is required,

$CHILD_STATUS.exitstatus.zero?

Recipes without an arguments key don't receive "default arguments"

When a recipe doesn’t have an :arguments key, the args variable defaults to config[:arguments], which does not include the command options -shell-escape and -interaction=batchmode. Thus, the command arguments are different between two scenarios which fall back to "default configuration":

  • there is no recipe: recipe[:arguments] becomes ['-halt-on-error', '-shell-escape', '-interaction=batchmode']
  • there is a recipe, but without an arguments key: recipe[:arguments] becomes ['-halt-on-error']

Instead, args can be set to config[:arguments] + config[:default_arguments], consistent with the default recipe that is created when recipe.length == 0.

Alternatively, use a single :arguments key to hold all the default arguments [‘-halt-on-error', '-shell-escape', '-interaction=batchmode’], and remove the :default_arguments key (and maybe extra_arguments too).

Was this going to be one of the fixes in rails-latex 3, mentioned in the "Regression fix" comment? Or perhaps, the extra_arguments key was intended to hold arguments such as the ones above.

no output

I have a small problem. I`m trying to make function which will generate a pdf. This is my code:

def generate_and_save_pdf
code = "\documentclass[12pt]{article}
\begin{document}
Don't forget to include examples of topicalization.
\end{document}"
@latex_config={:command => 'xelatex',:parse_twice => true}
LatexToPdf.generate_pdf(code, @latex_config, parse_twice = true)
end

In a log file I can see that "Output written on input.pdf (1 page).", but there is no input.pdf in tmp/rails-latex/ and I have no clue what is wrong.

Thanks in advance for help.

pdflatex not being invoked

Hi,

I have been using your gem for about 6 months, with Rails 3.0.7. It has worked great.

I am attempting to upgrade to Rails 3.2.2 and can not get it to generate PDF's. Instead, the app serves up unprocessed tex files to my browser, resulting in the browser (Chrome) displaying a "Failed to Load PDF Document" message.

Here's the response from Rails 3.0.7 (working):

ruby-1.9.2-p180 :004 > app.get "/user/pdf_cheque/2733"
Processing by Physical::UserController#pdf_cheque as HTML
Parameters: {"id"=>"2733"}
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
Rendered physical/user/cheque.pdf.erb within physical/layouts/application (1057.0ms)
Completed 200 OK in 1479ms (Views: 1090.4ms | ActiveRecord: 56.0ms)

Here's the response from Rails 3.2.2 (not working):

$ rails console
/home/directory/.rvm/gems/ruby-1.9.2-p180@va_rails_3.2.2/gems/actionpack-3.2.2/lib/action_dispatch/http/mime_type.rb:102: warning: already initialized constant PDF

I got rid of the the above warning by commenting out the corresponding line in the initializers/mime_types.rb

Loading development environment (Rails 3.2.2)

ruby-1.9.2-p180 :003 > app.get "/user/pdf_cheque/2733"
Processing by Physical::UserController#pdf_cheque as HTML
Parameters: {"id"=>"2733"}
... ActiveRecord output deleted ...
Rendered physical/user/cheque.pdf.erb within physical/layouts/application (357.7ms)
Completed 200 OK in 684ms (Views: 440.9ms | ActiveRecord: 32.9ms)
=> 200

Have you ever seen this sort of behaviour? Can you give me any pointers towards solving it?

Many thanks!

Syntax highlight

Not really an issue, but I made this sublime-syntax to use with this gem, is not perfect by any mean, but it looks a lot better when programming, I pretty much mix the html.erb one with the latex one, it depends on ruby.rails highlight and tex.latex to properly work, It has some issues, so any reviews are welcome, I don't know that much about syntax-highlight, so sorry about the details, here is the gist
https://gist.github.com/zerocool4u2/35a0ad2f362724323fd4890d9046a8a0, let me know if you found it helpful, regards
latex erb highlight

Usable on mac??

I tried to work with it on mac osx lion, but no success. I have installed MacTex package 1,8 gb size, but it seem not working. Maybe another tex package?? Thanks

pdflatex sometimes doesn’t exit, despite batchmode

An additional -halt-on-error will fix the problem. Sometimes TeX will print an error and wait for user input (although it shouldn’t) and sometimes it won’t give any error message at all. Regardless, exiting is still better than sitting indefinitely. I can attach a sample .tex file which Debian stable’s pdflatex neither renders nor exists with an error if you do not believe me.

Encoding

Hi there, and thanks for a great gem.

The undeclared encoding of lib/erb_latex.rb implies that the entire erb template is evaluated in US-ASCII. That becomes a problem when UTF-8 characters from some model are expanded in the template.

It's a simple error to not catch, as most data would be latex escaped, but I am entering latex code without escaping (because it is already escaped by RedCloth#to_latex), and therefore stopped.

A simple # encoding: UTF-8 on the first line does the trick.

Rails latex demo- not working (rails 3.1 ruby 1.9.2)

Hi I try to use you example but it seems not working. I am only beginner in rails so maybe it is my mistake but please give some advice. Here are some errors:
RuntimeError in Latex_example#index

From input.log
No such file or directory - pdflatex:
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/rails-latex-1.0.7/lib/rails-latex/latex_to_pdf.rb:28:in exec' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/rails-latex-1.0.7/lib/rails-latex/latex_to_pdf.rb:28:inblock in generate_pdf'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/rails-latex-1.0.7/lib/rails-latex/latex_to_pdf.rb:21:in fork' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/rails-latex-1.0.7/lib/rails-latex/latex_to_pdf.rb:21:ingenerate_pdf'
/media/Tomasko/Documents/Workspace_RoR/rails-latex-demo/app/views/layouts/application.pdf.erbtex:15:in _app_views_layouts_application_pdf_erbtex___1912624811996256140_20580820' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/template.rb:144:inblock in render'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.1.0/lib/active_support/notifications.rb:55:in instrument' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/template.rb:142:inrender'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/template_renderer.rb:52:in render_with_layout' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/template_renderer.rb:38:inrender_template'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/template_renderer.rb:12:in block in render' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/abstract_renderer.rb:22:inwrap_formats'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/template_renderer.rb:9:in render' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/renderer.rb:36:inrender_template'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_view/renderer/renderer.rb:17:in render' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/abstract_controller/rendering.rb:120:in_render_template'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_controller/metal/streaming.rb:250:in _render_template' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/abstract_controller/rendering.rb:114:inrender_to_body'
/home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_controller/metal/renderers.rb:30:in render_to_body' /home/tomasko/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.0/lib/action_controller/metal/compatibility.rb:43:inrender_to_body'
....

Many thanks for fast responce

Write logfiles to stdout rather than filesystem?

I am trying to use rails-latex on Heroku and am having some issues, so I would like to be able to see the logfiles. However, Heroku does not keep anything written to /app/tmp, so the only way to read this is if the log files are written to stdout so they are included in Heroku logs.

Is there a way to do this, or to write the contents of the log to screen?

Test fails

A test fails, because pdftotext produces the symbol rather than f and i on my system.

  1) Failure:
test_broken_doc_overriding_halt_on_error(TestLatexToPdf) [/home/user/projects/rails-latex/test/test_latex_to_pdf.rb:49]:
<"file with error\n\n1\n\n\f"> expected but was
<"file with error\n\n1\n\n\f">.

Rails 5

Allow for installation in Rails 5? Is it on the roadmap?

Escaping special characters like &

If a string to be rendered in erb contains & it generates a pdflatex error.
For example:

<%= @title %>

where @title contains &, gives an error.
Should escape special characters with .
If you do this manually, the & is converted to & due to html escaping.
A manual fix is to write @title.html_safe but that's not handy if you have a lot of strings.
Is there a way to fix these automatically?

Deploying on Heroku

I can't work with the gem on Heroku (or any other hosting service) that does not have the binary pdflatex installed on the system. However, for the case of Heroku they say that I can run binaries in my app/bin directory. So how to do that?

"Parse twice" default parameter issue

Problem:

When overriding the default parameter parce_twice (LatexToPdf.config.merge! :parse_twice => true), the TeX is parsed only once.

I believe the issue to be here:

def self.generate_pdf(code,config,parse_twice=nil)
parse_twice=config[:parse_twice] if parse_twice.nil?
config=self.config.merge(config)

In the current code, parse_twice is evaluated before merging config with the default configuration. The last two lines thus need to be interchanged.

Multiple-page table of contents

When I'm generating a PDF with a table of contents (using \tableofcontents) that is more than one page long, the page numbering is wrong because it doesn't take into account the table of contents itself.

For example if the table of contents is two pages long, all page numbers are off by 1. To be correct, the page numbering has to be incremented by 1.

To fix this issue extra compilation is needed. I'm already using "@latex_config={:command => 'pdflatex',:parse_twice => true}". I've managed to work around this by duplicating the line "system config[:command],'-draftmode',*args if parse_twice" in "rails-latex-1.0.11/lib/rails-latex/latex_to_pdf.rb".

Problem on Passenger 4

Passenger 4 is very sensitive if you try to manipulate STDOUT.

After I upgraded my system to Passenger 4, rails-latex stopped working and the input.log says:

can't change access mode from "rb+" to "a":
/home/rails/testapp/shared/bundle/ruby/1.9.1/bundler/gems/rails-latex-c230bc1d41a8/lib/rails-latex/latex_to_pdf.rb:30:in `reopen'

My system is stock Debian Wheezy. And I tried Passenger 4.0.5.

On the net I found that Passenger now uses STDOUT for its internal purposes, so we shouldn't touch it :)

regards!
Jakub

No pdf rendered

Hi. I am playing with the rails-latex-demo example.

In the root page "http://localhost:3000/latex_example" there is the link "print".
When I click on print, the browsers downloads the file latex_example.pdf, but it is not a pdf, instead this is its content:

\section*{Example \LaTeX\ to PDF document}

Generated at 2011-06-09 20:33:11 UTC.

\includegraphics{/var/www/rails/rails-latex-demo/public/example.jpg}

The last page is \pageref{LastPage}.

What is going wrong?
Thank you very much

Rails 6

Is there an issue with it?

Can't execute xelatex with -output-driver?

switching to Textlive 2021 i had to add the parameter -output-driver="xdvipdfmx -i dvipdfmx-unsafe.cfg -q -E" to my xelatex call. (https://www.tug.org/texlive/bugs.html , dvipdfmx)

while this testrun on the console works:

xelatex -etex -shell-escape -interaction=batchmode -output-driver="xdvipdfmx -i dvipdfmx-unsafe.cfg -E" eti2.tex

changing my rails-latex config to

    LatexToPdf.config.merge!  :recipe => [
        {
            :command => 'xelatex',
            :arguments => ['-etex', '-shell-escape', '-interaction=batchmode', '-output-driver="xdvipdfmx -i dvipdfmx-unsafe.cfg -q -E"'],
            :extra_arguments => [],
            :parse_runs => 2
        }
    ]

results in the following error:

Rendered lagerplatzs/etikettendruck.pdf.erb within layouts/lageretikett (38.4ms)
Running 'xelatex -etex -shell-escape -interaction=batchmode -output-driver="xdvipdfmx -i dvipdfmx-unsafe.cfg -q -E" input' in /home/lithos/lithos/tmp/rails-latex/397210-3798617907125015791 2 times...
-etex only works with -ini
sh: 1: /usr/local/texlive/2021/bin/x86_64-linux/xdvipdfmx -i dvipdfmx-unsafe.cfg -q -E: not found
Completed 500 Internal Server Error in 2302ms (ActiveRecord: 24.4ms)

the produced log file is incomplete (looks like it aborted in the middle of the run). running with the -output-driver parameter seems to omit the output on the console as well. maybe gets forked or something?

Prevent HTML escaping when rendering a pdf

When rendering a .pdf.erb file to latex and the file contains non-html-safe parts, they get escaped in a way that would be suitable for HTML. However, we are generating LaTeX and not HTML, which means html_safe makes no sense in the first place (already the name says that).

This is a problem in the following example:

<%= people.map{|person| "'#{person.label}'"}.join(', ') %> 

Since the string produced by join is not html_safe, the generated String is &quot;Anna&quot;, &quot;Berta&quot;, which then causes LaTeX to crash because the & sign is not permitted.

The alterantive would be to use safe_join and raw instead:

<%= safe_join people.map{|person| raw "'#{person.label}'"}, ', ' %> 

...or to tag the string as html_safe. However, it would be much more convenient, and also appropriate for the reasons described above, to skip HTML safety entirely when rendering LaTeX to PDF. Can this be achieved?

A simplest way to render plain TeX files (without processing them to PDF)

Hello, thanks a lot for your very useful work! :)

Please, advice me the simplest way to render just "text/tex", without PDF.
Now I use symlinking from all .pdf.erb views, partials and .pdf.erbtex layouts to their .tex.erb analogs. But this seems rather not-DRY... May it be somehow better? I mean, may be, some option on controller level?

Rails 7 support

This gem cannot be installed under Rails 7. Gemspec specifically excludes Rails 7.

Any chance of a Rails 7 release?
Given we now have 313 assertions failling out of 565, we really like this gem!

Thanks
André

Discovering Errors

I just spent hours figuring out that I had mismatched \begin{longtable} with \end{tabular}. I had to hack the source to make rails-latex not clean up its tmp directory to read the log and discover this.

Is there a better way I didn't notice?

add option to run latex 3 times

In complex documents (for example having table of contents across 2 or more pages) it is necessary to run latex 3 times to get proper page numbers.

It would be great to have such possibility in config... like "parse_trice" or so...

more complex example

hi Geoff,

could you provide a more complex example how to render explicit by calling the render method and how to send the file to a browser or show it inline!? i just cannot get rails-latex to work :-(

when i comment out the remove-dir-method, the input.pdf ist perfect but when it comes to showing or send the file - it is empty :-(

thanx a lot

How to save generated pdf file?

Hi. Thanks for your job! It helps me with my project but I need some advice. When I click on print it shows me generated pdf, its great but I need to save the document after clink on link to my server database. Where should I place some aditional code to save the file?? Thanks

RailsLatex::ProcessingError

When using this gem, it correctly generates the tex file but then displays : RailsLatex::ProcessingError.

I attach the logs for further information
input.log

Thanks in advance.

Using custom .cls and .sty files

Rather than installing .cls and .sty files in the system (I can't do that on Heroku for example), is there a way to pass an option to the template parser to copy some files from the app root beside the input.tex so that it works?

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.