Prevent Word Wrap In <TD> cell using tcpdf - tcpdf

I'm converting html tables into PDF's using TCPDF and want to prevent a single cell from wrapping text.
I have tried <td style='white-space: nowrap;> without luck.
Is there a way to force tcpdf to not wrap text within a single <td> tag?

you didn't close the single single quotes correctly, Please look
<td style='white-space: nowrap;>
Change this code to
$html ='<td style="white-space: nowrap;">';
then write this variable to pdf using
$this->pdf->writeHTML($html, true, false, true, false, '');

Related

How to use javascript variable in html table tag

I have declared a variable lenght in my javascript tag and now i want to use that 'length'
variable in html table tag how to use that in razor engine.
i have tried
<table>
<tr>
#for(var i=0;i<#lenght;i++)//this is not working
{some code}
</tr>
</table>
any suggestions?
If the length variable is defined in javascript, it only "lives" in the browser.
Razor code is executed on the server, so you cannot access this variable.
You could try to build the table in the browser using javascript.

How to display some html text from a database in a VIew within a TD cell, but only first 300 chars.....?

Not sure if this is possible, but I have some HTML in a DB column which I want to display in a Table TD cell in a Razor View. However the issue is that I only want the first 300 chars followed by "..."
ie:
<h2>My Test</h2>
<p>My Test description is very long</p>
So if I return the first 25 chars for the purpose of this question plus "...", I would get:
<h2>My Test</h2>
<p>My Tes ...
Which would then upset the containing page, due to the invalid HTML
ie
<table>
<tr>
<td>
<h2>My Test</h2>
<p>My Tes ...
</td>
</tr>
</table>
Is there a way round this?
At the moment I am using:
#Html.Raw(Model.myTestHtml)
to display the test HTML.
Perhaps I can only strip out the text from the HTMl and then .substring this.
Thanks appreciated.
If the html is not dynamic and it always follows same pattern, you may:
Parse the html to xml content
use LINQ2XML and find the node you want in there
edit that text and replace the additional parts with (...)
parse back to html
render it
LINQ2XML is very reliable. I am not sure if you can find dependencies which does same work as it with same level of accuracy and performance. but if you do find it, then you would not need to parsing in the process(to xml and from xml)

Create a table in Prawn by its html representation

Earlier Prawn gem allowed to create a table by its html representation (having an html table string as an input argument like <table class="abc"> .... </table>). Now I didn't find this facility in the manual.
So is it possible now? If not, is there any other option then?
TL;DR: if your use-case is 1) generating both HTML and PDF data (like online invoices etc.), and 2) making sure both look the same, then Prawn is not really the best solution (which is the same suggestion in the Prawn Readme).
In your case, you could parse the HTML using Nokogiri or Upton and extract the data from the HTML table and then use it to generate the PDF representation via Prawn. The HTML styles may not directly translate into the ones used by Prawn and so, even with a lot of code-wrangling, you might not achieve the consistency in styling — which I assume, from the comments on the answer by royalGhost, is the result you want. Also, a simple Nokogiri parsing solution won't work if your HTML table is nested and the parsing code does not cater to that. For example, consider this:
<table>
<tr>
<td>First Column, First Row</td>
<td>Second Column, First Row</td>
</tr>
<tr>
<table>
<tr>
<td>First Column, Second Row</td>
<td>Second Column, Second Row</td>
<td>Third Column, Second Row</td>
</tr>
</table>
</tr>
</table>
Then, in the Ruby parsing snippet, you should ensure that the inner <table>...</table> is parsed into a Prawn::Table object and not a row of Prawn::Table::Cell objects.
Any wkhtmltopdf based options such as WickedPDF or PDFKit offer much cleaner way of achieving the HTML to PDF conversion solution.
You have two options:
Ditch Prawn entirely and prefer the solution above.
Use Prawn by extracting the data from the HTML via Nokogiri/Upton and generate the PDF and not worry about styling being the same as that in the HTML representation.
Well you can use prawnto gem for templates to create table using prawn.
For e.g if you define the following templates, it will draw table with 3 columns with x, y and z width.
data = [ ["Column 1", "Column 2", "Column 3"] ]
table(data, :column_widths => [x,y,z], :cell_style => { :inline_format => true })

how can i fetch the tr of this structure in nokogiri of in html response [duplicate]

This question already has answers here:
Parse table using Nokogiri
(3 answers)
Closed 8 years ago.
how can i fetch the tr which mention in code of this structure in nokogiri of in html response
<html>
<body>
<table>
</table>
<table>
<tbody>
<tr>
<td>
<table>
<tr></tr>
<tr><td> wanna this text as output.</td></tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
I prefer using the CSS accessors as they're more forgiving. Using your HTML sample, I'd use:
irb(main):026:0> doc.search('table table tr').last.text
" wanna this text as output."
Alternately, the XPATH accessor is:
irb(main):042:0> doc.search('//table//table//tr').last.text
" wanna this text as output."
You're looking for the nested table containing multiple rows. You want the last row.
Be wary whenever you look at the HTML inside a browser and see a <table><tbody> combination. Browsers do a lot of code fix-up, which can result in tags that are not there when you retrieve the HTML directly and pass it into a parser. And, those non-existing tags you see in the browser's output will throw off your CSS or XPath access when you add them in. In particular, <tbody> is a real common problem.
There are multiple ways to access that text, for instance:
html = Nokogiri::HTML my_html_string
row_text = html.at('//table[2]/tbody/tr/td/table/tr[2]').text

Clicking on image using RSpec & Capybara

I'm using Rspec and Capybara.
I am attempting to click an image, but cannot find a way to get Capybara to click the image. I'm not sure what to do, as I have in fact same class, alt and src.
Thanks
Example of html
<tr class="even">
<td class="gridData" style="text-align:center;">
<td class="gridData"> 221 </td>
<td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
<td class="gridData">
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
</td>
</tr>
<tr class="odd">
<td class="gridData" style="text-align:center;">
<td class="gridData"> 222 </td>
<td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
<td class="gridData">
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
In fact here is the source, which I want to test:
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
But as I said, I cannot find a way to get Capybara to click the image.
Any ideas?
You can use this:
page.first(".user-info-grid").click
This is use to find user-info-grid class and apply click function.
If multiple classes are comes with this name then the click will execute in first found class.
You can use that when looking for an id:
find("#the_id").click
Or for a class
find(".the_class").click
I put this and works:
find("img[src*='myphoto.jpg']").click
That's because your image is an image, not a link. Wrap it inside a link first, then click the link, like so:
<img ... />
Do I understand correctly that there's no <a> element because you're using a JavaScript onclick handler? If so, then you need to be aware that plain Capybara doesn't do JavaScript. You need to use capybara-webkit, Selenium, or something else similar.
But I'd advise you to put in the <a> element, and apply the JS handler to that. That will make it easier for user agents without JavaScript to handle (yes, there still are some!).
And Matthias is right: make your HTML valid.
Try using XPath, if not that then you can use either visit_to the source of the link, or you can use try help on http://railscasts.com/episodes/257-request-specs-and-capybara

Resources