<pre> is displaying MySQL text as like without <pre> in TCPDF - tcpdf

I want like to surround text from MySQL in <pre> tags. Here is my code -
$html=$html.'<pre>'.$database[$i]['response'].'</pre>';
But when viewed in a browser, it is not behaving as expected, as there are no new lines and spacing is not preserved.
Does anyone know why this isn't working?

The <pre> tag will works fine in TCPDF, please try this,
$html .='<pre> '.$database[$i]['response'].' </pre>';
Or
$html .='<pre> echo $database[$i]['response'] </pre>';

Please upgrade your TCPDF library it will start working.
You can see more pre tag example in TCPDF here
Note: <pre> tag will not work inside <td> tag.

Related

Empty alt with Rails image_tag

I have a number of images that need an empty alt attribute, that is alt="", using image_tag.
I tried the code in the accepted answer at image_tag - Is there a way to make the alt attr blank by default? but it results in <img alt> not <img alt="">.
Short of removing image_tag and using img can this be done?
Ok this is a non-issue. Alejandro's comment on the original question prompted me to check the source, I had previously only checked in devtools inspector.
The source is alt="" using the linked code, it's alt in Chrome's DOM.
Thanks for suggestions, sorry for any trouble.

HtmlPurifier - Codeblock

I was looking in HtmlPurifier documentation, but I can't see nothing about that.
Let's say I have
<div class="codebox">
All html tags here - Even <div class="codebox">another code box</div>
</div>
I want to parse the content of the first <div class="codebox"> so it can be readable as plaintext.
Can htmlpurifier do that ?
Out of the box HTMLPurifier can't do that and there is no config setting, that I know of, that can convert only the first <div> tag to plain text without converting the entire document. And even for converting the entire document to text the HTMLPurifier is neither needed nor recommended.
You can extend functionality of HTMLPurifier but unless you are an expert coder, I wouldn't recommend doing that.
However if you want to convert a part of the HTML document to text then break it into parts and run the part which you want to convert to text through
strip_tags()
PHP Manual page on strip_tags
You could convert all the div tags in your document to plain text with this configuration directive:
$config->set(HTML.ForbiddenElements, 'div'); //This will black list 'div' tag
And if you absolutely insist on converting your entire document to text using HTMLPurifier then here is the config directive that will do that.
$config->set('HTML.Allowed', ''); //This will white list NO tags ''

Redactor-Rails html tags showing

I'm trying to implement redactor as a WYSIWYG editor with ruby on rails. Everything seems to be working fine except that when I edit text in the editor the html tags show up. This happens even when I use the html button on the toolbar.
So on the webpage the text appears something like this:
<p>Edited text here</p>
I haven't included any code because I'm not really sure where to begin looking with this so any help at all will be appreciated :)
when using a text editor you have to tell your rails app that the area is html safe.
This is (by default) not the case as people could attack your site by using a text box you have put into your app.
by declaring an area as html safe you should be able to use the html tags as you like.
be aware of the security risk for using this.
e.g.
<div class="description">
<%= #foo.foo_desc.html_safe%>
</div>
Hope this clears it up for you.
in your view try using raw before the text you are trying to show. For example
<%= raw #post.body %>
this will work out with the html tags and show the processed text only without the tags.

Use of html in textarea not working correct

My cms is actievadmin and just installed tinymce for editing in the textarea. When i make changes (bold, paragraph tags ect) the page showing raw html. In the DB is stored with the html but is not rendering the html.
Does anyone know what this problem is?
Try replacing the output on your page to something like this:
<%=raw #model.content %>

newline characters screwing up <pre> tags (Ruby on Rails)

I developing a blog and some really annoying stuff is happening with newline characters (\n). Everything works fine except if I make a post that contains pre tags my newline characters screw up the indentation.
So if I have code that looks like this
<pre>
<code>
some code some code
more code more code
</code>
</pre>
For some reason the newline characters that are saved in the db field with the post are causing whatever is inside the pre tag to be indented by a tab or two.
I have no idea why it's doing it, but if I do something like
string.gsub!(/\n/, "<br />")
The indentation is removed, so I know it has to do with the \n. But then my problem is that there are way too many line breaks and the format is then way off.
So then I tried to capture everything inside the pre tags with a method that looks like this
def remove_newlines(string)
regexp = /<pre>\s?(.*?)\s?<\/pre>/
code = regexp.match(string)
code[1].gsub!(/\n/, "<br />")
end
But I can't get that to work properly.
Anyone know how I can rid of this weird indentation problem, or any pointers on this?
Thanks!
It sounds like your template engine is auto-indenting the contents of the <pre> tags. Browsers render the whitespace inside <pre> tags as it is (and so they should, according to specs). This means that the whitespace at the beginning of each line inside the <pre> added by the template engine in order to make the HTML source more readable is rendered in the actual page as well, unlike whitespace most other places in HTML source.
The solution therefore depends on your templating language.
If you are using HAML:
HAML FAQ: How do I stop Haml from indenting the contents of my pre and textarea tags?
Hope this helps.

Resources