wicked_pdf doesn't work -- Ruby on Rails - html

I want to generate PDF from a html doc in a rails app. So I decided to use wicked_pdf. But its not working. I tried to integrate it in an app, but it did not work. So I thought of trying to see if it works in the rails console then i will integrate it in the app.
Here's what I came across in the console
wp = WickedPdf.new
=> #<WickedPdf:0x4e7eea0 #exe_path="C:\\wkhtmltopdf\\wkhtmltopdf.exe">
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
pdf = wp.pdf_from_string HTML_DOCUMENT
"***************C:\\wkhtmltopdf\\wkhtmltopdf.exe - - ***************"
NotImplementedError: fork() function is unimplemented on this machine
from C:/Ruby/lib/ruby/1.8/open3.rb:57:in `fork'
from C:/Ruby/lib/ruby/1.8/open3.rb:57:in `popen3'
from C:/Users/raw/Desktop/html/scheduler/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
from (irb):3
UPDATE: I am using Windows7
I am not able to figure out where I am going wrong.
Please help.
Thanks in advance.

Install the gem win32-open3
gem install win32-open3
And now Change line:6 in lib/wicked_pdf.rb
require 'open3'
to
require 'win32/open3'
and line:20 in the same file
command_for_stdin_stdout = "#{#exe_path} #{options} - - -q"
to
command_for_stdin_stdout = "#{#exe_path} #{options} - -"
and now in console I get
wp = WickedPdf.new
=> #<WickedPdf:0x4e65f70 #exe_path="C:\\wkhtmltopdf\\wkhtmltopdf.exe">
?> HTML_DOCUMENT = "<html><body>Hello World</body></html>"
=> "<html><body>Hello World</body></html>"
?> pdf = wp.pdf_from_string HTML_DOCUMENT
"***************C:\\wkhtmltopdf\\wkhtmltopdf.exe - - ***************"
=> "%PDF-1.4\n1 0 obj\n<<\n/Title (\376\377)\n/Producer (wkhtmltopdf)\n/CreationDate (D:20101127124137)\n>>\nendobj\n4 0 obj\n<<\n/Type /ExtGState\n/SA true\n/SM 0.02\n/ca 1.0\n/CA 1.0\n/AIS false\n/SMask /None>>\nendobj\n5 0 obj\n[/Pattern /DeviceRGB]\nendobj\n8 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n6 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Contents 9 0 R\n/Resources 11 0 R\n/Annots 12 0 R\n/MediaBox [0 0 595 842]\n>>\nendobj\n11 0 obj\n<<\n/ColorSpace <<\n/PCSp 5 0 R\n/CSp /DeviceRGB\n/CSpg /DeviceGray\n>>\n/ExtGState <<\n/GSa 4 0 R\n>>\n/Pattern <<\n>>\n/Font <<\n/F7 7 0 R\n>>\n/XObject <<\n>>\n>>\nendobj\n12 0 obj\n[ ]\nendobj\n9 0 obj\n<<\n/Length 10 0 R\n/Filter /FlateDecode\n>>\nstream\nx\234\245\222QKÄ0\f\200ßó+ò,\×6ëÖ‚øàPÁa\254àƒø ;O9\274Ãy\017\376}ÓtÇ�\003\213`ÖåKÒ\264ɪ\273áß\016XuÃ'\216\263îÐJ\273\220\027j\226ÕX\257Èê\264ÐRM+tÜÁ\204\023ôÐó;é Ž\273j\221ø\207*×›\243\022Ý�\017\r)ÞÁ\023\233\037K\223È©`\264v\236\271>7Sð;<^à¾Xêè1\"KÏA\271ÐÖ\244\255\225\2643\223o$\224j$\207_\257\260IçþKÚï\247Édèøë-Þó\263ŧg\206ëâ5\256#T\267-\032\217qÃ÷\220\241d\025\271\205\270\2625Æ5^òPÌÆ-\230䜉\025\022N\200\004P\001Ôç)N#}\002M.ã
9\255\000W(ãÜDil\376qLî¢ùo3Ó ÊØÃ\0177Ч\201endstream\nendobj\n10 0 obj\n271\nendobj\n13 0 obj\n<< /Type /FontDescriptor\n/FontName /QNAAAA+ArialRegular\n/Flags 4 \n/FontBBox [-594.726562 -290.527343 1790.03906 900.390625 ]\n/ItalicAngle 0 \n/Ascent 651.367187 \n/Descent -188.476562 \n/CapHeight 0 \n/StemV 65.4296875 \n/FontFile2 14 0 R\n>> endobj\n14 0 obj\n<<\n/Length1 3840 \n/Length 17 0 R\n/Filter /FlateDecode\n>>\nstream\nx\234\245W]l\024×>wgg×C0ÆkW\204p\rÄ?\024l\263Æ»ÆÄ8ÈD\245\030cc\e\0220\224\214wf\275cvgV\263\263^\257\241\205\2406BQ#\245\205\246\025êC‰\2226I[~ÒÚ”\246\250M\245<\264\225ÒH\225\252\242ÊM\213\232\206(U\037\"\265Ä?=÷ÜYgL(RÕ]Í�ïœ{î9ßùæÞY\030\000\224ÁiP\000ú‡Z¢+\242Gsèù*^O\216\245\213Ék\237Áø
Take a look at this blog post for understanding it.
Thanks.

You can try DocRaptor.com. It's an online app that lets you convert html to pdf. They have free accounts available, plus paid accounts if you need a lot of docs.
Hope that helps!
Julie

create a folder in your public called "pdfs"
Controller:
pdf = render_to_string :pdf => "graphs", :orientation => 'Landscape', :font_size => 11
# then save to a file
save_path = Rails.root.join('pdfs', filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
graphs.rhtml
View:
add this line of code for styles
<%= wicked_pdf_stylesheet_link_tag "css filename here" %>

Related

Filebeat upload JSON templates to ElasticSearch

In short
I need to load several JSON templates to ElasticSearch within filebeat.yaml configuration
I have
Directory with templates:
-rootdir
|
| - templates
|
|- some-template.json
|- some-2-template.json
|- some-3-template.json
Pre-setup properties in filebeat.yaml configuration, like:
setup.template:
json:
enabled: true
path: /rootdir/templates
pattern: "*-template.json"
name: "json-templates"
This is actually a blueprint as I do not know how to load to ElasticSearch all templates, because one template using this config loaded successfully, if I append to path, for example, /some-template.json.
After the starting the Filebeat I've got next error logs:
ERROR [publisher_pipeline_output] pipeline/output.go:154 Failed to connect to backoff(elasticsearch(http://:9200)): Connection marked as failed because the onConnect callback failed: error loading template: error reading file /rootdir/templates for template: read /rootdir/templates: is a directory
Question is
How I can upload multiple files within one property with different index-patterns in each template, so the results after running GET _cat/templates?v=true should be like this:
name index_patterns order version composed_of
some-template [some*] 0 7140099
some-2-template [some-2*] 0 7140099
some-3-template [some-3*] 0 7140099
.monitoring-es [.monitoring-es-7-*] 0 7140099
.monitoring-alerts-7 [.monitoring-alerts-7] 0 7140099
.monitoring-logstash [.monitoring-logstash-7-*] 0 7140099
.monitoring-kibana [.monitoring-kibana-7-*] 0 7140099
.monitoring-beats [.monitoring-beats-7-*] 0 7140099
ilm-history [ilm-history-5*] 2147483647 5 []
.triggered_watches [.triggered_watches*] 2147483647 12 []
.kibana-event-log-7.16.3-template [.kibana-event-log-7.16.3-*] 0 []
.slm-history [.slm-history-5*] 2147483647 5 []
synthetics [synthetics-*-*] 100 1 [synthetics-mappings, data-streams-mappings, synthetics-settings]
metrics [metrics-*-*] 100 1 [metrics-mappings, data-streams-mappings, metrics-settings]
.watch-history-12 [.watcher-history-12*] 2147483647 12 []
.deprecation-indexing-template [.logs-deprecation.*] 1000 1 [.deprecation-indexing-mappings, .deprecation-indexing-settings]
.watches [.watches*] 2147483647 12 []
logs [logs-*-*] 100 1 [logs-mappings, data-streams-mappings, logs-settings]
.watch-history-13 [.watcher-history-13*] 2147483647 13 []
Additionally
I'm running Filebeat and ElasticSearch in Docker using Docker compose, may be it would be helpful somehow
Thank you in advance!
Best Regards, Anton.

Django: Display a PDF document in iframe or embed not working

I want to display a PDF on on a page in our website. The URL is correct and pointing to the PDF. I am seeing a display, but I see garbage characters instead of the PDF.
In template using:
<embed src="{{packet.certificate_pdf.url}}" ...>
<iframe src="{{packet.certificate_pdf.url}}" ...>
Then I see results on the page where the PDF is suppose to display:
%PDF-1.3
%âãÏÓ
1 0 obj
<<
/Type /Pages
/Count 6
/Kids [ 3 0 R 4 0 R 5 0 R 6 0 R 7 0 R 8 0 R ]
>>
endobj
2 0 obj
<<
/Producer (PyPDF4)
>>
endobj
3 0 obj
<<
/MediaBox [ 0 0 595 842 ]
/Type /Page
/Parent 1 0 R
/Contents [ 10 0 R ]
/Resources <<
/ProcSet [ /PDF /Text /ImageC ]
/XObject <<
/HiQPdf_mlpnjcimdfejkpcopajmiphnnjaiocbm 11 0 R
>>
>>
>>
endobj
4 0 obj
<<
/MediaBox [ 0 0
.... etc
Using in Production enviroment:
Django 3.2
Python 3.8
Already using: X_FRAME_OPTIONS = 'SAMEORIGIN'
I also tried converting to base64 then displaying it:
<iframe src="data:application/pdf;base64,{{pdf_in_base64}} ...
This used to work, but recently Chrome sometimes show the PDF and sometimes not, which I can't solve. Something in the base64 seems to break the PDF display intermittendly. So now I am trying to use URL directly instead of base64.
Anyone an idea?

mPDF error on Codeigniter

I tried to convert a html page to pdf and was decided to use mPDF, I follow what the documentation does. When running the code, it does not prompt out the PDF to ask for save. Btw I get those error code.
Here is the code from Controller.
//this data will be passed on to the view
$data['the_content']='mPDF and CodeIgniter are cool!';
//load the view, pass the variable and do not show it but "save" the output into $html variable
$html=$this->load->view('ajax/pdf_output', $data, true);
//this the the PDF filename that user will get to download
$pdfFilePath = "the_pdf_output.pdf";
//load mPDF library
$this->load->library('m_pdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->load();
//generate the PDF!
$pdf->WriteHTML($html);
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output($pdfFilePath, "I");
Below is the result i get:
%PDF-1.4 %���� 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x��P]O�#���㓚����^�1�h�7�C��B(h���sW�Fs����vvv�B')�ձCgha�6��Mp�6� �H�U[P��{��-[�uz��#��뮉�r�#Υ�9�R���'�J�h&���e� �J�YW�f����\���/�m�Ӷ�����J.w���j��N�ގ��^�=f!��ƲO����o�92yh�m���9� �e��[��#�3���?u�R%_�¿�)�X|jt2H׆��+��S��™9%�R��:��ƒ7��m��Z����9n� endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> stream /CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo <> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def 1 begincodespacerange <0000> endcodespacerange 1 beginbfrange <0000> <0000> endbfrange endcmap CMapName currentdict /CMap defineresource pop end end endstream endobj 9 0 obj <> endobj 10 0 obj < >> /FontFile2 12 0 R >> endobj 11 0 obj <> stream x����V�����Qfd%{dT*�l-�Ȉ��_����s��ڥ����}��#;���t���:��Nt�S�n�3�m�s��B��h��JW���nt�[��Nw�^�{��&�l�GM7�l�{���z��^4��^��7��]���B�-��J���V[�K�}�[}�G?��f���V���n���w�� endstream endobj 12 0 obj <> stream x��| \TU��9��;�.�zqIe�]ce�t��apfQQ�q�qGKSS+5-K+Ӟ�z�6�35���gi���wν3d=��}����g�s�=������\0�B��4#+76�r����=\��}�O��:�p <7�r���G~BHH���EŖ�j��B�C_U��W /� q<��U�}��<�!��t�Qo��ۀPdwXN���<���X���<�~]��Po3oC(��_��gW��Ўz��r��8�XR!<����+�vG�B4�Q��� ����Z�<���(��^�Dhk��#��r'WQ����h4�F��i�Fw���H$�]�3�$77kBi(��e��A�=7�"凨����C���]D��.=�onf�577���l��$��y#����? ��CA(�G!(���#��:�Ѓ��uE݀�p�"Qw�z�����F}P4��bP,����8�EP<���h����h�yQJF)H�R�(4��1h,JG(e�l4��\��ƣ|4MD�#�S�T4 =���H:�Ev$���-�|�tdF3�r��w�\�����>�����~�N��0OZ�ˀ���;0����>�H����m� $Z�|�^t�E��r�Uo�l/�ì-���a^� �$#�΢O�b�A5h ��L�]��tx1�� ��$d�Y�Y�� Jf��,��W�qo�Cf�"<$�a2z�"�8>=�'�˧� s��*/z[�*�{��#��x���op:.��O�A{�?p�Yz����������-ޡ��!y�� }kP&� T�]��H"0���� Qi�c �&�oL ���Q򒏡�c���������Ҥc�C�H��11*�� ��=6{�|��.EE��H��܉�dO� �����[ ,~O�5�֓��І,>�lozG��s�� ���6Mo��^�m���� 0P�!,T�#DK�"�����SS�ku��#�%�33�̐�hz��I�z�v�zNG��`���N���"�a�[
Anyone can tell me what happening with this?
it looks like the "I" parameter is causing trouble because the browser doesn't recognize your file
according to the docu you've the following Possibilities:
I: send the file inline to the browser. The plug-in is used if available. The name given by $filename is used when one selects the “Save as” option on the link generating the PDF.
D: send to the browser and force a file download with the name given by $filename.
F: save to a local file with the name given by $filename (may include a path).
S: return the document as a string. $filename is ignored.
try something like that :
$pdf->Output($pdfFilePath, "D");
die;
or on the other hand you can try to add some header to tell the browser explicitly this is a pdf document
header('Content-Type: application/pdf');
$pdf->Output($pdfFilePath, "I");
die;
because it could be CIs outpout class overwrites MPDF's header (but this is just a hunch)
$html=$this->load->view("ajax/pdf_output",$data,true);
//load mPDF library
$this->load->library('m_pdf');
//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);
//download it.
ob_clean();
$this->m_pdf->pdf->Output($pdfFilePath,'F');
check your folder.....
If you want show download dialogue your need to place below code
$filename = time()."_order.pdf"; //your file name
$html = $this->load->view('unpaid_voucher2',$data,true);
/// $data variable is your dynamic data if you have no dynmic data then you can pass empty instead of variable like.
$html = $this->load->view('unpaid_voucher2','',true);
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);
//For download pass D and save on server pass F.
$this->m_pdf->pdf->Output("./uploads/".$filename, "D");
Here is full configuration to integrate mpdf into codeigniter
The string is a binary PDF representation and its presence means Content-type: application/pdf header is not sent correctly or it is
overriden by your code or setup. Most likely by text/plain or text/html.
Try to figure out these:
Are you resetting Content-type header in PHP code somewhere after calling the mPDF Output method?
Is your server forcing a different Content-type somewhere in your setup?
Does your browser support displaying application/pdf Content-type directly?
Given that the D Output mode gives you the same result, I'd guess the Content-type header is being overriden somewhere after calling the Output method, presumably by CodeIgniter.

Database update weirdness

I am developing a game in Django in which the quantity of rebels updates after a turn change. Rebels are represented as integers from 0 to 100 in the (MySQL) database.
When I save, this happens:
print world.rebels
>>> 0
rebstab = 0
world.rebels += rebstab
world.save()
print world.rebels
>>> 0
However, when I use F() expressions (as I gather I should to prevent race conditions), this happens:
print world.rebels
>>> 0
rebstab = 0
world.rebels = F('rebels') + rebstab
world.save()
print world.rebels
>>> 100
What's going on?
I don't have any experience with using F() objects like this so this answer may not be useful, but having a look at the documentation it mentions:
In order to access the new value that has been saved in this way, the object will need to be reloaded:
reporter = Reporters.objects.get(pk=reporter.pk)
so you may have to actually query the DB again to see the updated value:
print world.rebels
>>> 0
rebstab = 0
world.rebels = F('rebels') + rebstab
world.save()
print World.objects.get(pk=world.pk)
>>> 100
Edit: Also look [at the example where you don't need to pull the objects into memory and can do everythign at the DB level:
World.objects.get(pk=...).update(rebels=F('rebels) + 1)
I just figured out the reason had nothing to do with the F() object itself but rather a conflict with a custom save method. See Django F() objects and custom saves weirdness for a more indepth explanation.

Create line chart from csv file without excel

Suppose i have the following data in csv format :
Time Total Allocated Deallocated
0.00004 0 16 0
0.000516 16 31 0
0.046274 47 4100 0
0.047036 4147 0 31
0.047602 4116 35 0
0.214296 4151 4100 0
0.215109 8251 0 35
i am looking for some kind of software that will allow me to make a line chart of it ( where time column will be the X axis) , i used excel for now , but i am looking for something else,that will allow me to see in greater details .
Any ideas ?
Use Datawrapper. It's very easy and you can publish it on the web or export it to a PNG file.
You can also use R. Here is an example of code to generate a time series plot :
library("ggplot2")
df <- data.frame(date = seq(as.Date("2012-01-01"),as.Date("2012-12-01"), by = "month"), x = rnorm(12))
ggplot(df, aes(x=date, y = x)) + geom_line() + theme_bw()
This is an old question but still: https://plot.ly is also a good site for that kind of stuff.