Issue with generating a PDF file using pdfkit in ruby rails application - html

I am trying to generate a basic PDF file with some data in it.
html = ApplicationController.new.render_to_string("invoices/invoice", layout: false)
kit = PDFKit.new(html, :page_size => 'Letter')
pdf = kit.to_file("#{Rails.root}/")
How to solve below error? or is there a better example that I use as a reference
2.7.4 :123 > html = ApplicationController.new.render_to_string("invoices/invoice", layout: false)
Rendering invoices/invoice.html.erb
Rendered invoices/invoice.html.erb (Duration: 0.0ms | Allocations: 4)
=> "<div style=\"width:100%;\">\n <p> Testing PDF Generation</p>\n</div>\n"
2.7.4 :124 > kit = PDFKit.new(html, :page_size => 'Letter')
=> #<PDFKit:0x0000000107ed4290 #source=#<PDFKit::Source:0x0000000107ed4268 #source="<div style=\"width:100%;\">\n <p> Testin...
2.7.4 :125 > pdf = kit.to_file("#{Rails.root}/")
/private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf: /private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf: cannot execute binary file
Traceback (most recent call last):
1: from (irb):125
PDFKit::ImproperWkhtmltopdfExitStatus (Command failed (exitstatus=126): /private/var/nameee/Desktop/test/test_application/bin/wkhtmltopdf --quiet --page-size Letter --margin-top 0.75in --margin-right 0.75in --margin-bottom 0.75in --margin-left 0.75in --encoding UTF-8 - /private/var/nameee/Desktop/test/test_application/)
More Info:
ruby 2.7.4p191
Rails 6.1.6.1
gem 'pdfkit', '~> 0.8.2'

Related

LuaLaTeX with LuaSQL on Archlinux

I am working on a LuaLaTeX file which would take data from my database using LuaSQL. So this are the 003-v1.tex and 003-v1.lua files that I came up with:
003-v1.tex file:
\documentclass{article}
\usepackage{luacode}
% Lua kodo vpišemo v ločeno datoteko zaradi syntax highlithing
\directlua{dofile('003-v1.lua')}
\newcommand{\stranke}{\luadirect{stranke()}}
\begin{document}
\begin{tabular}{ll}
\hline
id stranke & ime \\
\hline
\stranke
\hline
\end{tabular}
\end{document}
003-v1.lua file:
function stranke ()
package.cpath = package.cpath .. ";/usr/lib/i386-linux-gnu/lua/5.1/?.so"
luasql = require "luasql.mysql"
env = assert (luasql.mysql())
con = assert (env:connect("linux_krozki","root","mypassword"))
cur = assert (con:execute("SELECT * FROM stranke"))
vnos = cur:fetch ({}, "a")
while vnos do
print(
string.format([[%s & %s \\]], vnos.id_stranke, vnos.ime)
)
vnos = cur:fetch (vnos, "a")
end
end
This files ought to work but when I try to compile using lualatex 003-v1.tex I get error:
This is LuaTeX, Version 1.0.4 (TeX Live 2017/Arch Linux)
restricted system commands enabled.
(./003-v1.tex
LaTeX2e <2017-04-15>
(using write cache: /home/ziga/.texlive/texmf-var/luatex-cache/generic)(using r
ead cache: /var/lib/texmf/luatex-cache/generic /home/ziga/.texlive/texmf-var/lu
atex-cache/generic)
luaotfload | main : initialization completed in 0.144 seconds
Babel <3.12> and hyphenation patterns for 1 language(s) loaded.
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo(load luc: /home/ziga/.texlive/
texmf-var/luatex-cache/generic/fonts/otl/lmroman10-regular.luc)))
(/usr/share/texmf-dist/tex/lualatex/luacode/luacode.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/share/texmf-dist/tex/luatex/luatexbase/luatexbase.sty
(/usr/share/texmf-dist/tex/luatex/ctablestack/ctablestack.sty))) (./003-v1.aux)
003-v1.lua:8: module 'luasql.mysql' not found:
no field package.preload['luasql.mysql']
[kpse lua searcher] file not found: 'luasql.mysql'
[kpse C searcher] file not found: 'luasql.mysql'
no file '/usr/local/lib/lua/5.2/luasql.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './luasql.so'
no file '/usr/lib/i386-linux-gnu/lua/5.1/luasql.so'
stack traceback:
[C]: in function 'require'
003-v1.lua:8: in function 'stranke'
[\directlua]:1: in main chunk.
\luadirect ... { \luacode#maybe#printdbg {#1} #1 }
l.14 \stranke
And according to this topic this error arrizes because LuaLaTeX can't load module luasql.mysql while lua can on its own. How do I know this? If I comment out first line (function stranke ()) and last line (end) from 003-v1.lua before compiling with lua 003-v1.lua I get an output which is completely fine:
1 & Žiga \\
2 & Ranja \\
3 & Romana \\
So my question is, how to make sure that module luasql.mysql loads when LuaLateX is called? I am on Archlinux and am using texlive. I heard that people compile the texlive again with support for luasql, but can't find the step by step guide... That would be awesome! It would be even better if there is anyone who already compiled it.
Here is the info about my Texlive version:
[ziga#laptop ~]$ pacman -Qs tex | grep live
local/texlive-bibtexextra 2017.44915-1 (texlive-most)
local/texlive-bin 2017.44590-2
local/texlive-core 2017.44918-1 (texlive-most)
local/texlive-fontsextra 2017.44818-1 (texlive-most)
local/texlive-formatsextra 2017.44177-2 (texlive-most)
local/texlive-games 2017.44131-1 (texlive-most)
local/texlive-humanities 2017.44833-1 (texlive-most)
local/texlive-langchinese 2017.44333-1 (texlive-lang)
local/texlive-langcyrillic 2017.44895-1 (texlive-lang)
local/texlive-langextra 2017.44908-1 (texlive-lang)
local/texlive-langgreek 2017.44917-1 (texlive-lang)
local/texlive-langjapanese 2017.44914-1 (texlive-lang)
local/texlive-langkorean 2017.44467-1 (texlive-lang)
local/texlive-latexextra 2017.44907-1 (texlive-most)
local/texlive-music 2017.44885-1 (texlive-most)
local/texlive-pictures 2017.44899-1 (texlive-most)
local/texlive-pstricks 2017.44742-1 (texlive-most)
local/texlive-publishers 2017.44916-1 (texlive-most)
local/texlive-science 2017.44906-1 (texlive-most)
We found an answer on Archlinux forums after this thread was posted. It looks like there are some internal problems with Lua language - package.cpath wasn't able to reckognize the questionmark, so ?.so had to be changed into mysql.so. Can anyone explain why this happened?

Trouble while extending CKAN template

I was extending a ckan template as per:
http://docs.ckan.org/en/latest/theming/templates.html
So when I add {% ckan_extends %} in an empty index.html file in my extension to use the original index.html I get the following exception and traceback(My debug is set to true):
WebError Traceback:
⇝ ValueError: invalid literal for int() with base 10: ''
View as: Interactive (full) | Text (full) | XML (full)
Module ckan.controllers.home:88 in index view
>> return base.render('home/index.html', cache_force=True)
Module ckan.lib.base:174 in render view
>> return cached_template(template_name, render_template)
Module pylons.templating:249 in cached_template view
>> return render_func()
Module ckan.lib.base:128 in render_template view
>> return render_jinja2(template_name, globs)
Module ckan.lib.base:85 in render_jinja2 view
>> return template.render(**extra_vars)
Module jinja2.environment:989 in render view
>> return self.environment.handle_exception(exc_info, True)
Module jinja2.environment:748 in handle_exception view
>> traceback = _make_traceback(exc_info, source_hint)
Module jinja2.debug:140 in make_traceback view
>> return translate_exception(exc_info, initial_skip)
Module jinja2.debug:182 in translate_exception view
>> lineno = template.get_corresponding_lineno(tb.tb_lineno)
Module jinja2.environment:1056 in get_corresponding_lineno view
>> for template_line, code_line in reversed(self.debug_info):
Module jinja2.environment:1072 in debug_info view
>> self._debug_info.split('&')]
ValueError: invalid literal for int() with base 10: ''
Help needed.

Load an FXML form using JRuby

I am trying to load an FXML form from a JRuby script. I can successfully do that from a Jython script. This is the basic Jython code:
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.scene import Scene
class Main(Application):
def start(self, stage):
main_form = self.getClass().getResource('/main.fxml')
self.loader = FXMLLoader(main_form)
root = self.loader.load()
scene = Scene(root)
stage.setScene(scene)
stage.show()
args = []
print Main().class
print args
Application.launch(Main().class, args)
The script executes fine and the form gets shown. The output of the script is:
<type 'org.python.proxies.__main__$Main$0'>
[]
So Main.class() is type 'org.python.proxies.main$Main$0'
This is my attempt to create the equivalent JRuby script:
java_import javafx.application.Application
java_import javafx.fxml.FXMLLoader
java_import javafx.scene.Scene
class Main < Application
attr_accessor :loader, :fxml_form, :scene
def start(stage)
#fxml_form = self.java_class.getResource('/main.fxml')
#loader = FXMLLoader.new(#fxml_form)
root = #loader.load()
#scene = Scene.new(root)
stage.set_scene(#scene)
stage.show()
end
end
args = Array.new()
puts Main.new().java_class
puts args
Application.launch(Main.new().java_class, args)
The output of this script is:
javafx.application.Application
NameError: no method 'launch' for arguments (org.jruby.javasupport.JavaClass,org.jruby.RubyArray) on Java::JavafxApplication::Application
available overloads:
(java.lang.String[])
(java.lang.Class,java.lang.String[])
(root) at /home/uros/NetBeansProjects/JRuby-JavaXF/lib/main.rb:31
So Main.new().java_class is javafx.application.Application and the form never gets loaded. My FXML in both cases is a basic 'hello world' form:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="264.0" prefWidth="315.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" prefHeight="34.0" prefWidth="89.0" text="Button" />
</children>
</StackPane>
I am on Linux Mint 17. My Jython is 2.7b3 and my JRuby is 1.7.16.1.
[EDIT]
Additional information:
When trying to use the jrubyfx gem like this:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launch
I get the following exception:
NameError: cannot initialize Java class javafx.scene.control.ListView
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/core_ext/precompiled.rb:320
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
require at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55
glob at org/jruby/RubyDir.java:242
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:313
load_dsl at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx/dsl.rb:312
require at org/jruby/RubyKernel.java:1065
(root) at /home/uros/.rvm/rubies/jruby-1.7.16.1/lib/ruby/gems/shared/gems/jrubyfx-1.1.0-java/lib/jrubyfx.rb:37
(root) at /home/uros/NetBeansProjects/RubyApplication4/lib/main.rb:1
My Java version:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)
NameError: cannot initialize Java class javafx.scene.control.ListView
was caused by
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
That JRubyFX error was fixed in 1.1.1 (which added java 8 support). 1.1.0 only supported Java 7 (after u6). It should work now using the code you gave in both 7u6+ and 8:
require 'jrubyfx'
class Main < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml "main.fxml"
show
end
end
end
Main.launch

Nokogiri truncating HTML for some pages, not others

We are using Nokogiri to parse data from iTunes. On some pages, it works. On others, it fails and truncates the page mysteriously.
Our code:
# Get iTunes HTML for app bundle
itunes_url = 'https://itunes.apple.com/us/app-bundle/id918236019'
uri = URI.parse itunes_url
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Get.new uri.request_uri
resp = http.request req
# Covnert HTML into XML for parsing
bundle_xml = Nokogiri.XML resp.body
bundle_xml.remove_namespaces!
#puts "ERRORS: #{bundle_xml.errors}"
puts "ORIGINAL\n=============\n#{resp.body}\n\n\n\n============="
puts "NOKO\n=============\n#{bundle_xml}"
Even though errors are returned for other iTunes pages, Nokogiri is able to parse the page properly. Basically, most of the elements after a certain element are removed mysteriously by Nokogiri.
Resp.body output: https://gist.github.com/anonymous/33ecfe82e3d22a39375a
Nokogiri output: https://gist.github.com/anonymous/7622ef92bf430889b9f4
i18n (0.6.11, 0.6.9, 0.6.5, 0.6.4, 0.6.1)
io-console (0.3)
journey (1.0.4)
jquery-rails (3.1.1, 3.1.0, 3.0.4, 2.2.1)
json (1.8.1, 1.8.0, 1.7.7, 1.5.5)
kgio (2.8.1, 2.8.0)
mail (2.4.4)
mime (0.4.0, 0.2.0, 0.1)
mime-types (1.25.1, 1.25, 1.24, 1.23, 1.21)
mini_portile (0.6.0, 0.5.3, 0.5.2, 0.5.1)
minitest (2.5.1)
mongo (1.10.0, 1.9.2, 1.9.1)
mongo_mapper (0.12.0)
mongoid (3.1.6)
moped (1.5.2, 1.5.1)
multi_json (1.10.1, 1.9.2, 1.9.0, 1.8.4, 1.8.2, 1.8.0, 1.7.9, 1.7.7, 1.6.1)
mysql2 (0.3.16, 0.3.15)
newrelic_rpm (3.9.0.229, 3.7.3.204, 3.7.2.192, 3.6.6.147)
nokogiri (1.6.1, 1.6.0)
By trying this myself, I think I see the problem. You're parsing an HTML response.
Change
bundle_xml = Nokogiri.XML resp.body
to:
bundle_xml = Nokogiri.HTML resp.body
and see if this works for you.
The HTML parser is much more lenient and handles missing closing tags, etc.
The HTML has invalid markup. Nokogiri says:
>> #doc.errors
[
[0] #<Nokogiri::XML::SyntaxError: htmlParseEntityRef: no name>,
[1] #<Nokogiri::XML::SyntaxError: htmlParseEntityRef: no name>,
[2] #<Nokogiri::XML::SyntaxError: Tag nav invalid>
]
That can make content disappear as Nokogiri attempts to fix up the HTML to make it usable.

How to create a rails 4 offline webapp?

I'm trying to create an app with Rails 4 that should be usable without internet connection.
I've heard about html5 application cache and the rack-offline gem which is the approach I took.
Now, it seems that it's not working properly on Rails 4 since the /application.manifest only shows:
CACHE MANIFEST
# dd1ba6bba9339ef83f9c1225c70289dd6326d3caae01b0d52b502381030dc78f
404.html
422.html
500.html
NETWORK:
*
Also, I'm using assets precompile so the application.js, application.css and the image files has a fingerprint in their names, something like application-e8cc2fba8275c884c.js.
I made my own solution in a generate_appcahe_manifest.rake file and put it in /lib/tasks folder.
task :generate_appcache_file => ['deploy:precompile_assets', 'html5_manifest']
desc "Create html5 manifest.appcache"
task :html5_manifest => :environment do
puts 'Creating appcache manifest file...'
File.open("public/manifest.appcache", "w") do |f|
f.write("CACHE MANIFEST\n")
f.write("# Version #{Time.now.to_i}\n\n")
f.write("CACHE:\n")
assets = Dir.glob(File.join(Rails.root, 'public/assets/**/*'))
assets.each do |asset|
if File.extname(asset) != '.gz' && File.extname(asset) != '' && File.extname(asset) != '.json'
filename_path = /#{Rails.root.to_s}\/public\/(assets\/.*)/.match(File.absolute_path(asset))[1].to_s
# f.write("assets/#{File.basename(asset)}\n")
f.write(filename_path.concat("\n"))
end
end
f.write("\nNETWORK:\n")
f.write("*\n")
f.write("http://*\n")
f.write("https://*\n")
end
puts 'Done.'
end
namespace :deploy do
task :precompile_assets do
require 'fileutils'
if File.directory?("#{Rails.root.to_s}/public/assets")
FileUtils.rm_r "#{Rails.root.to_s}/public/assets"
end
puts 'Precompiling assets...'
puts `RAILS_ENV=production bundle exec rake assets:precompile`
puts 'Done.'
end
end
So when I run rake generate_appcache_file on the terminal, I got a /public/manifest.appcache file with the compiled assets like this:
CACHE MANIFEST
# Version 1409045103
CACHE:
assets/app/backgrounds/strips-05561384267a3286ab382c852f1e8b0d.jpg
assets/app/backgrounds/team-12e0fc5f670794c4eba3db117fba6746.jpg
assets/app-a7de6b02d1d39b783fe4e7ab7906ec95.css
assets/app-ae958853aa13f9011e35121cbe5b3cfe.js
NETWORK:
*
http://*
https://*
Finally, I call that file on my /app/views/layouts/app.html.erb file:
<!DOCTYPE html>
<html lang="en" manifest="/manifest.appcache">
More info about offline application cache that helps me a lot can be found here.