Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am using QTextDocument to generate pdf from html string. However, the white space all eaten except one left.I write the html string to file and open it.The white space also disappeared.

Only if I type the & nbsp directly in html file,it's OK.
In QString, it's not.

What I have tried:

It's very strange that QDomDocument auto add "amp" before each "nbsp".So the white space place holder can't take effect.


C++
 QTextDocument text_document;
//here parse the html file , make some changes
 QDomDocument html_doc = ParseHtmlToNodes(path);
    if(html_doc.isNull())
        return;
  QFile file("changed.html");
    if (!file.open(QIODevice::WriteOnly)) {
        qDebug()<< "Cannot open file for writing: "
                  << file.errorString();
        return;
    }


    QTextStream stream_saver(&file);
    stream_saver<<html_doc.toString();
    stream_saver.setCodec("UTF8");
    stream_saver.flush();
Posted
Updated 19-Mar-17 23:55pm
v6

Try
C#
myhtml.replace(" " , "&nbsp;");

or
C#
QByteArray myhtml = temp_name.toLatin1().replace(" " , "&nbsp;");
 
Share this answer
 
v2
Comments
Charly LEE 19-Mar-17 23:21pm    
If I do this, the html will display "nbsp; ",but not white space
Patrice T 19-Mar-17 23:26pm    
It shouldn't, something else fo wrong.
Charly LEE 20-Mar-17 0:52am    
I got it, In code, I add some text to a html /, not using outside.
Charly LEE 20-Mar-17 4:36am    
Hi,I update the question. new findings appended. Do you know the tricky thing in QDomDocument? I actually did what you've written, but something automatically happened when I wrote file.
Patrice T 20-Mar-17 4:54am    
Can't help you on this.
According to the HTML specification multiple white spaces has to be ignored if not inside a PRE block:
In particular, user agents should collapse input white space sequences when producing output inter-word space.
The Qt implementation just follows the standard.

Open your HTML file with a browser and you will see that it is rendered in the same way.

If you need a different rendering with multiple spaces, you have to format your HTML input accordingly (e.g. by using &nbsp; non-breaking spaces or PRE blocks).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900