There are situations where you find this error when rendering PDF documents using DOMPDF library in Codeigniter. It becomes confusing when it works on localhost but breaks when moved to an online server.
This code works fine when the Attachment is set to true but fails when false.
$dompdf->loadHtml($html_content);
$dompdf->render();
$dompdf->stream($document_title, ["compress" => 1, "Attachment" => true]);
%PDF-1.7 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R /F2 9 0 R >> /XObject << /I1 13 0 R /I2 14 0 R /I3 17 0 R >> /ExtGState << /GS1 10 0 R /GS2 11 0 R /GS3 15 0 R /GS4 16 0 R >> >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text /ImageC ]
I fixed it by adding exit(); at the bottom of the code.
$dompdf->loadHtml($html_content);
$dompdf->render();
$dompdf->stream($document_title, ["compress" => 1, "Attachment" => false]);
exit();
Related
(1..2) <=> 3 # => -1
(-2..21) <=> -10 # => 1
(-2..21) <=> 0 # => 0
Is there a ruby implemented method already for this function? Otherwise, I would code it myself.
You can utilize clamp:
3.clamp( 1..2 ) <=> 3 # => -1
-10.clamp(-2..21) <=> -10 # => 1
0.clamp(-2..21) <=> 0 # => 0
in general:
number.clamp(range) <=> number
To get the "distance": (as stated in your original question)
number - number.clamp(range)
You can add nonzero? to get nil instead of 0:
(number - number.clamp(range)).nonzero?
For example:
def distance(range, number)
(number - number.clamp(range)).nonzero?
end
distance(1..2, 3) #=> 1
distance(-2..21, -10) #=> -8
distance(1..4, 3) #=> nil
def where_is_val?(range, val)
case val
when range
0
when ..range.begin
-1
else
1
end
end
where_is_val?(0..10, -1) #=> -1
where_is_val?(0..10, 0) #=> 0
where_is_val?(0..10, 5) #=> 0
where_is_val?(0..10, 10) #=> 0
where_is_val?(0..10, 15) #=> 1
where_is_val?(0...10, 10) #=> 1
where_is_val?('c'..'m', 'a') #=> -1
where_is_val?('c'..'m', 'c') #=> 0
where_is_val?('c'..'m', 'f') #=> 0
where_is_val?('c'..'m', 'm') #=> 0
where_is_val?('c'..'m', 'z') #=> 1
where_is_val?('c'...'m', 'm') #=> 1
Notice that this works with both inclusive (0..10) and exclusive (0...10) ranges.
Suppose that I have a matrix Eigen::Matrix<double, 3, 3> whose entries are
1 2 3
4 5 6
7 8 9
How can I expand it to
1 2 3 0
4 5 6 0
7 8 9 0
0 0 0 1
I need this to multiply 3D affine/projective transformations (4 by 4, under the type Eigen::Transform) with 3D rotation matrices (3 by 3).
You want conservativeResize:
Eigen::MatrixXf mat;
mat.resize(3,3);
mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
std::cout << mat << "\n\n";
mat.conservativeResize(4,4);
mat.col(3).setZero();
mat.row(3).setZero();
mat(3, 3) = 1;
std::cout << mat << "\n\n";
I have a 2d map which is an array of arrays:
map = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0]]
I also have a list with moves:
moves = [[0,0], [0, 1], [1, 1]]
I want to print the movement on console (but I want every time to overwrite previous output, like this)
So the expected output should be something like this
* 0 0 0 0 0 * 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 * 0 0 0
0 1 1 1 1 --> 0 1 1 1 1 --> 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I have tried some things but I can't get close to my desired output.
To clear the screen and wait for ENTER, try this:
map = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0]]
loc = [0,0]
moves = [[0,0], [0, 1], [1, 1]]
def display_map(map,loc)
system 'clear'
loc.first.times { puts }
map.each { |row| print ' '*loc.last; p row }
end
moves.each do |x,y|
loc[0] += x
loc[1] += y
display_map(map,loc)
gets
end
This works on a Mac. For other OS's you may have to replace system 'clear' with system 'cls'.
[Edit: I see I misunderstood the question. I think this is what you want:
moves.each do |x,y|
system 'clear'
nrows.times do |i|
ncols.times do |j|
print (i==x && j==y) ? '*' : map[i][j]
print ' ' if j < ncols-1
end
puts
end
gets
end
You can use ANSI terminal escape codes.
Example:
# Save initial cursor position
puts "\033[s"
puts <<EOF
* 0 0 0 0
0 0 0 0 0
0 1 1 1 1
0 0 0 0 0
EOF
sleep 1
# Restore initial cursor position
puts "\033[u"
puts <<EOF
0 * 0 0 0
0 0 0 0 0
0 1 1 1 1
0 0 0 0 0
EOF
sleep 1
# Restore initial cursor position
puts "\033[u"
puts <<EOF
0 0 0 0 0
0 * 0 0 0
0 1 1 1 1
0 0 0 0 0
EOF
Following #Cary_Swoveland's solution with clearing the console, I manage to do it like this:
map = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0]]
moves = [[0,0], [0, 1], [1, 1]]
def display_map(map,loc)
system "clear" or system "cls"
# loc.first.times { puts }
map.each { |row| p row }
end
moves.each do |x,y|
map[x][y] = 8
display_map(m,loc)
map[x][y] = 0
gets
end
array = Array.new(10) { Array.new(10 , 0)}
array.each { |x| print x }
Prints out one single line of ten [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].
If I were to change print to puts, I then get 100 0 down the page.
How do I print out each array on a separate line without the "[]" and ","?
Something like:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Suppose:
arr = Array.new(10) { (0..20).to_a.sample(10) }
Then
puts arr.map { |x| x.join(' ') }
1 9 6 15 7 19 18 3 0 12
13 20 18 15 0 3 19 1 14 16
7 16 5 3 12 19 4 9 20 10
6 10 9 1 18 17 7 19 5 15
12 3 8 16 10 5 2 18 20 6
12 9 0 18 2 11 16 8 7 15
8 9 14 19 3 16 6 20 13 17
7 19 16 14 13 6 9 2 3 5
10 17 8 15 11 2 13 14 16 7
14 9 20 17 15 3 4 2 11 19
is not very, er, attractive. For something more pleasing, you could quite easily do something like this:
width = arr.flatten.max.to_s.size+2
#=> 4
puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join }
1 9 6 15 7 19 18 3 0 12
13 20 18 15 0 3 19 1 14 16
7 16 5 3 12 19 4 9 20 10
6 10 9 1 18 17 7 19 5 15
12 3 8 16 10 5 2 18 20 6
12 9 0 18 2 11 16 8 7 15
8 9 14 19 3 16 6 20 13 17
7 19 16 14 13 6 9 2 3 5
10 17 8 15 11 2 13 14 16 7
14 9 20 17 15 3 4 2 11 19
If you have too many columns to display on the screen you can do this:
puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join.tinyfy }
1 9 6 15 7 19 18 3 0 12
13 20 18 15 0 3 19 1 14 16
7 16 5 3 12 19 4 9 20 10
6 10 9 1 18 17 7 19 5 15
12 3 8 16 10 5 2 18 20 6
12 9 0 18 2 11 16 8 7 15
8 9 14 19 3 16 6 20 13 17
7 19 16 14 13 6 9 2 3 5
10 17 8 15 11 2 13 14 16 7
14 9 20 17 15 3 4 2 11 19
Try join:
array.each { |x|
puts x.join(" ")
}
# prints:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Print a 2-dimensional array with spacing between columns
Accepts a 2d array of objects that have a to_s method (strings integers floats and booleans..) and an optional margin width integer.
Update: It now works with arrays of varying lengths.
def print_table(table, margin_width = 2)
# the margin_width is the spaces between columns (use at least 1)
column_widths = []
table.each do |row|
row.each.with_index do |cell, column_num|
column_widths[column_num] = [column_widths[column_num] || 0, cell.to_s.size].max
end
end
puts (table.collect do |row|
row.collect.with_index do |cell, column_num|
cell.to_s.ljust(column_widths[column_num] + margin_width)
end.join
end)
end
Note: the parenthesis after the puts statement is required so table.collect and the do end block are not passed as two separate parameters to the puts method.
Example table
my_table = [
["1", "Animal", "Dog", "1"],
[1, "Animal", "Cat", "2"],
[1, "Animal", "Bird", "3"],
[2, "Place", "USA", "1"],
[2.5, "Place", "Other", "2"],
[3, "Color", "Red"],
[3, "Color", "Blue", "b"],
[3, "Some more color", "Orange", "c"],
[4.7, "Age", "Young", "a"],
[4, "Age", "Middle", "b"],
[4, "Age", "Old", "c"],
[5, "Alive"],
[],
[5, "Alive", false, "n"]
]
print_table my_table
Prints:
1 Animal Dog 1
1 Animal Cat 2
1 Animal Bird 3
2 Place USA 1
2.5 Place Other 2
3 Color Red
3 Color Blue b
3 Some more color Orange c
4.7 Age Young a
4 Age Middle b
4 Age Old c
5 Alive
5 Alive false n
(Not colored. The coloring above was added by StackOverflow.)
You may want to write your own method to do it. Something like:
def array_2D_print array
array.each do |arr|
arr.each do |item|
print "#{item} "
end
print "\n"
end
end
If you only use this once in your code, you might also consider not creating any method:
array.each do |arr|
arr.each do |item|
print "#{item} "
end
print "\n"
end
This solution has the advantage of being easier to modify than other alternatives, to match what you want to print.
Say we have array of arrays:
tree_limbs = Array.new
tree_limbs << %w(1 2 3 4 5 7)
tree_limbs << %w(1 2 3 4 6)
tree_limbs << %w(1 2 3 8 9 10 11)
tree_limbs << %w(1 2 3 8 9 10 12)
What's the effective way to build such a hash tree within ruby:
tree_hash = {1 =>
{2 =>
{3 =>
{4 =>
{5 =>
{7 => nil}
},
{6 => nil}
},
{8 =>
{9 =>
{10 =>
{11 => nil},
{12 => nil}
}
}
}
}
}
}
If you definitely want explicit nil's on the last level then you can do something like that:
tree_limbs = Array.new
tree_limbs << %w(1 2 3 4 5 7)
tree_limbs << %w(1 2 3 4 6)
tree_limbs << %w(1 2 3 8 9 10 11)
tree_limbs << %w(1 2 3 8 9 10 12)
tree_hash = {}
tree_limbs.each do |path|
path.each_with_index.inject(tree_hash) do |node, (step, index)|
if index < path.size - 1
node[step] ||= {}
else
node[step] = nil
end
end
end
p tree_hash
#=> {"1"=>{"2"=>{"3"=>{"4"=>{"5"=>{"7"=>nil}, "6"=>nil}, "8"=>{"9"=>{"10"=>{"11"=>nil, "12"=>nil}}}}}}}