XHtml to stream export

General TRichView support forum. Please post your questions here
Post Reply
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

XHtml to stream export

Post by wvd_vegt »

Hi,

Is there support for exporting xhtml into a stream so I can stream it directly into an embeddedWB from EuroMind without saving to disc first?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

rv.SaveHTMLToStreamEx
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Thanks

For people interested the following code works like charm:

Code: Select all

procedure T_TaakEditor.DoPreview(rve: TRichViewEdit);
var
  ms                 : TMemoryStream;
begin
  ms := TMemoryStream.Create;

  rve.SaveHTMLToStreamEx(ms,
    ChangeFileExt(Application.ExeName, '.xhtml'),
    'A Title', '', '', '', '',
    [rvsoUTF8, rvsoXHTML]);
  ms.Seek(0, soFromBeginning);

  emBeddedWB1.LoadFromStream(ms);
end;
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

When i stream from TRichViewEdit into EmebbedWb the images are saved on disk. Hyperlinks are present in the generated xhtml but lack path & file:// protocol.

Is there a way to correct this?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

TRichView does not save hyperlinks in HTML itself. It's done in OnWriteHyperlink event. Modify code in this event to add protocol prefixes when necessary
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

This event isn't fired during the SaveHTMLToStreamEx call. Any suggestions?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, it's not possible.
If this event is not fired, all hyperlink would be written as a plain text (hyperlink target is provided by this event, or by obsolete OnURLNeeded)
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

Perhaps i need to clarify, i dropped an image from the file system into a TRichViewEdit and call the streaming function.

The output is:

Code: Select all

<img alt="" hspace="1" vspace="1" src="Pim.xhtml13.jpg" />

I did some digging and found that OnHtmlSaveImage is called but for it to work i need to manually save the Image but can't get the Filename from the ItemNo supplied.

I Also found that setting doDefault to true saves the image to disc (as i want to) but also resets the filename written into the html generated.
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Preview content in an EmbeddedWB including disc based Images

Post by wvd_vegt »

Hi,

Finnaly figured it out. The trick lies in the OnSaveImage Event but needs to duplicate some code from TCustomRVData.DoSavePicture.

Only minor flaw is that the OverrideFileNames parameter isn't available so I had to set it to false (but as the calling program passes it in, it's also known inside the eventhandler).

Result is an image save to disc and a correct file:/// type url in the generated xhtml.


The OnSaveImage2 eventhandler:

Code: Select all

procedure TXHtmlEditFrame.RichViewEdit1SaveImage2(Sender: TCustomRichView;
  Graphic: TGraphic; SaveFormat: TRVSaveFormat; const Path,
  ImagePrefix: string; var ImageSaveNo: Integer; var Location: string;
  var DoDefault: Boolean);
var
  ext               : string;
  fn                : string;
begin
  if SaveFormat = rvsfHTML then
    begin
      ext := '.jpg';
      if RV_IsHTMLGraphicFormat(Graphic) then
        ext := '.' + GraphicExtension(TGraphicClass(Graphic.ClassType));
    end
  else
    ext := '.bmp';

  fn := Sender.RVData.GetNextFileName(ImagePrefix, Path, Ext, ImageSaveNo, false); //no OverrideFileNames

  if (SaveFormat = rvsfHTML) and
    ((Graphic is TJpegImage) or RV_IsHTMLGraphicFormat(Graphic)) then
    Graphic.SaveToFile(fn);

//Sample: src="file:///C:/Bureaublad/Joe_s_Garage.gif"

  Location := 'File:///' + StringReplace(fn, '\', '//', [rfReplaceAll]);

  DoDefault := False;
end;
And the calling routine that previews TRichViewEdit content in an EmbeddedWB including disc based images:

Code: Select all

procedure T_TaakEditor.DoPreview(rve: TRichViewEdit);
var
  ms                : TMemoryStream;
begin
  ms := TMemoryStream.Create;

  rve.RVData.Flags := rve.RVData.Flags + [rvflRoot];
  rve.SaveHTMLToStreamEx(ms,
    ExtractFilePath(Application.ExeName),
    'A Title', '', '', '', '',
    [rvsoUTF8, rvsoXHTML]);
  ms.Seek(0, soFromBeginning);

  Preview.LoadFromStream(ms);
end;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

That's right, but you initially asked about hyperlinks, not images :)
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

My excuses for mixing them up again. I tend to treat everything with a url as a hyperlink.

But is it possible to change the routines a bit so you can save the image more easily by calling the code already present in TRichView? I find copying peices code to archieve a minor enhancement awkward as the copied piece may change for next releases of a library like TRIchView.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

procedure TForm3.RichViewEdit1SaveImage2(Sender: TCustomRichView;
  Graphic: TGraphic; SaveFormat: TRVSaveFormat; const Path,
  ImagePrefix: String; var ImageSaveNo: Integer; var Location: String;
  var DoDefault: Boolean);
begin
  Location := Sender.RVData.SavePicture(SaveFormat, ImagePrefix, Path, ImageSaveNo, True,
    clWhite, Graphic);
  Location := 'file://' + StringReplace(Path+Location, '\', '/', [rfReplaceAll]);
  DoDefault := False; 
end;
Corrected: '\' should be changed to '/', not to '//'
Last edited by Sergey Tkachenko on Fri Sep 23, 2005 9:23 am, edited 1 time in total.
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

Thanks, works like charm (and is much shorter).
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I have corrected the example
Post Reply