SaveHTMLEx with inline CSS and Middle Only not saving correctly

General TRichView support forum. Please post your questions here
Post Reply
Deety
Posts: 4
Joined: Mon Jun 08, 2020 2:16 pm

SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Deety »

Hi,

I'm trying to insert an RTF element from our application into a wider HTML output. I'm using the RichViewEdit.SaveHTMLEX (18.01) to try and do this.

I'm using a Bootstrap CSS for the output in general, but wanted to override locally for the RTF, so I used Middle Only and Inline CSS options on the SaveHTMLEX. However, it's not picking up the CSS properties for things like form size, background colour, text colour, text decoration etc.

I've tried it from the demo application and it works great, so I know it's something I'm doing.

My code for converting RTF to HTML looks like this:

Code: Select all

function TfrmSTShellSheet.RTFToHTML(aRTF: TSTDBRTFEdit; aName: string): string;
var
    oRTF     : TRichViewEdit;
    oStyle   : TRVStyle;
    oHTML    : TStringList;
    sRTFFile : string;
    sHTMLFile: string;
begin
    Result := '';
    if Length(aRTF.RTFText) = 0 then
        exit;

    sRTFFile  := HTMLTempRTFFile(aName);
    sHTMLFile := HTMLTempHTMLFile(aName);
    oRTF      := TRichViewEdit.Create(sfpMain);
    oStyle    := TRVStyle.Create(sfpMain);
    try
        oRTF.Visible := False;
        oRTF.Parent  := sfpMain;
        oRTF.Style   := oStyle;

        // Shift the RTF into a file to be loaded into RVEdit
        aRTF.SaveTerFile(sRTFFile);
        oRTF.Clear;
        oRTF.LoadRTF(sRTFFile);
        oRTF.Format;

        // Save the middle section with inline CSS
        oRTF.SaveHTMLEx(sHTMLFile, aName, 'img' + fsHTMLFileName + aName, '', '', '', [rvsoInlineCSS, rvsoMiddleOnly]);

        // Load it into a string list to include in the wider page
        oHTML := TStringList.Create;
        try
            oHTML.LoadFromFile(sHTMLFile);
            Result := oHTML.Text;
        finally
            oHTML.Free;
            DeleteFile(PChar(sHTMLFile));
        end;

    finally
        oRTF.Free;
    end;
end;
and it's producing HTML that looks like this:

Code: Select all

<p style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;"><span style=" font-size: 10pt; font-family: 'Arial', 'Helvetica', sans-serif; font-style: normal; font-weight: normal; color: #000000; background-color: transparent; text-decoration: none;">This text is larger and in red.</span></p>
<p style="text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;"><span style=" font-size: 10pt; font-family: 'Arial', 'Helvetica', sans-serif; font-style: normal; font-weight: normal; color: #000000; background-color: transparent; text-decoration: none;">Background is pale pink</span></p>
Could you tell me what astoundingly obvious thing I'm missing...?
Deety
Posts: 4
Joined: Mon Jun 08, 2020 2:16 pm

Re: SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Deety »

And I've just noticed that it's still using table attributes that have been depreciated in HTML-5 (border, cellpadding, cellspacing, width and valign)... not that big an issue. But worth considering for future releases.
Sergey Tkachenko
Site Admin
Posts: 17253
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Sergey Tkachenko »

Sorry, I do not understand the first question.
Which attributes are not included? I can see all of them. Probably, it makes sense to add an option to include only CSS attributes that are different from TextStyles[0].

As for table saving, probably some attributes may be removed. However, it will increase HTML a lot, because instead of saving one cellpadding for a table we will need to save padding for every table cell. This problem can be solved by introducing table styles, but it is only planned for future.
Deety
Posts: 4
Joined: Mon Jun 08, 2020 2:16 pm

Re: SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Deety »

The text should have looked like this:

This text is larger and in red.
Background is pale pink

The information for this is not in the final HTML, instead it says: background-color: transparent; text-decoration: none; etc

But...on further hunting, I've discovered that the LoadRTF appears to be where the formatting disappears.

I can see that the formatting has disappeared if I do the following and look at the saved RTF file.

Code: Select all

        oRTF.Clear;
        oRTF.LoadRTF(sRTFFile);
        oRTF.Format;
        oRTF.SaveRTF(sRTFFile, False);
My initial RTF has the colour and size data in it. So I suspect that if I can get the loadRTF to work then the SaveHTMLEX will work.
Sergey Tkachenko
Site Admin
Posts: 17253
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Sergey Tkachenko »

Assign
oRTF.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
oRTF.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded;
Deety
Posts: 4
Joined: Mon Jun 08, 2020 2:16 pm

Re: SaveHTMLEx with inline CSS and Middle Only not saving correctly

Post by Deety »

Perfect.

That was the bit I was missing.

You're a hero!
Post Reply