amagical-net / rails-latex Goto Github PK
View Code? Open in Web Editor NEWrails-latex is a renderer for rails which allows tex files with erb to be turned into an inline pdf
License: MIT License
rails-latex is a renderer for rails which allows tex files with erb to be turned into an inline pdf
License: MIT License
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.
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.
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.
Hi. I use your gem. It works perfect, but I have some issues with render to string.
usage of:
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.
nice work!
i would like to render latex to png without having to convert the pdf to png
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
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.
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"
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?
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":
recipe[:arguments]
becomes ['-halt-on-error', '-shell-escape', '-interaction=batchmode']
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.
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.
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
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!
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
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
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.
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.
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:in
block 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:in
generate_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:in
block 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:in
render'
/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:in
render_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:in
wrap_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:in
render_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:in
render_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:in
render_to_body'
....
Many thanks for fast responce
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?
A test fails, because pdftotext
produces the symbol fi
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">.
Allow for installation in Rails 5? Is it on the roadmap?
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?
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?
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.
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".
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
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
Is there an issue with it?
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?
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 "Anna", "Berta"
, 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?
e.g. with \usepackage{lastpage} it is necessary to preprocess the tex-file with "latex"
is it possible to provide an option for preprocessing!?
thanx
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?
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é
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?
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...
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
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
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.
$?.exitstatus
can be sometimes nil
, see: https://ruby-doc.org/core-2.5.0/Process/Status.html
Traceback form my code:
NoMethodError: undefined method `zero?' for nil:NilClass File "/usr/local/bundle/gems/rails-latex-2.2.2/lib/rails-latex/latex_to_pdf.rb" line 85 in generate_pdf`
rails 5.2.0 rails-latex 2.2.2 ruby 2.5.1
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.