creating a new function HtmlToRtf()

creating a new function HtmlToRtf()

Postby Silvio.Falconi » Thu Feb 15, 2024 1:12 pm

I 'm trying to create a new function Html to Rtf but I have problem to converte table tag

Image


Code: Select all  Expand view


#include "FiveWin.ch"

FUNCTION Main()
   LOCAL cRtf, oDlg
   LOCAL cString := ""
   
   // Insert the HTML code directly into the cString variable
   cString += "This is a complex text, colored and formatted by Fivewin. <br><br>"
   cString += "<font color=#FF0000>RED</font><br>"
   cString += "<font color=#008800>GREEN</font><br>"
   cString += "<font color=#0000FF>BLUE</font><br><br>"
   cString += "<b>Bold Text</b> <br>"
   cString += "<i>Italic Text</i> <br>"
   cString += "<u>Underlined Text</u> <br>"
   
   // Add a Table 2 row 2 columns
   cString += "<table border='1'>"
   cString += "<tr><td>Header 1</td><td>Header 2</td></tr>"
   cString += "<tr><td>Cella 1</td><td>Cella 2</td></tr>"
   cString += "</table><br>"
   
   // add a  link
   cString += "<a href='http://www.example.com'>Link</a><br>"
   
   // Convert HTML code to RTF format
   cRtf := HtmlToRtf(cString)

   //Create a dialog box to show formatted text

   TestRtfM(cRtf)

RETURN NIL



FUNCTION HtmlToRtf(cString)
   LOCAL cRtf := ""
   LOCAL cTemp := cString

   cTemp := StrTran(cTemp, "<html>", "")
   cTemp := StrTran(cTemp, "<body>", "")
   cTemp := StrTran(cTemp, "</html>", "")
   cTemp := StrTran(cTemp, "</body>", "")

   // Sostituisci i tag HTML con codici RTF corrispondenti
   cTemp := StrTran(cTemp, "<br>", "\line ")
   cTemp := StrTran(cTemp, "<b>", "{\b ")
   cTemp := StrTran(cTemp, "</b>", "\b0}")
   cTemp := StrTran(cTemp, "<i>", "{\i ")
   cTemp := StrTran(cTemp, "</i>", "\i0}")
   cTemp := StrTran(cTemp, "<u>", "{\ul ")
   cTemp := StrTran(cTemp, "</u>", "\ulnone}")
   cTemp := StrTran(cTemp, "<font color=#FF0000>", "{\cf1 ")
   cTemp := StrTran(cTemp, "<font color=#008800>", "{\cf2 ")
   cTemp := StrTran(cTemp, "<font color=#0000FF>", "{\cf3 ")
   cTemp := StrTran(cTemp, "</font>", "}")

   // Aggiungi il supporto per le tabelle
   cTemp := StrTran(cTemp, "<table border='1'>", "\pard\intbl\itap0\plain\ltrpar{\trowd\trgaph70\trleft-70")
   cTemp := StrTran(cTemp, "<tr>", "\trbrdrt\brdrdb\cell ")
   cTemp := StrTran(cTemp, "</tr>", "\cell\cell\row}")
   cTemp := StrTran(cTemp, "<td>", "")
   cTemp := StrTran(cTemp, "</td>", "\cell")
   cTemp := StrTran(cTemp, "border='1'", "\brdrw10\brdrs")
   cTemp := StrTran(cTemp, "</table>", "\par}")

   // Aggiungi il supporto per i link
   cTemp := StrTran(cTemp, "<a href=", " {\field{\*\fldinst{HYPERLINK ")
   cTemp := StrTran(cTemp, "</a>", "}}{\fldrslt ")
   cTemp := StrTran(cTemp, ">", "")

   // Altri tag HTML supportati
   cTemp := StrTran(cTemp, "<h1>", "{\fs40 ")
   cTemp := StrTran(cTemp, "</h1>", "\line }")
   cTemp := StrTran(cTemp, "<h2>", "{\fs32 ")
   cTemp := StrTran(cTemp, "</h2>", "\line }")
   cTemp := StrTran(cTemp, "<h3>", "{\fs24 ")
   cTemp := StrTran(cTemp, "</h3>", "\line }")
   cTemp := StrTran(cTemp, "<p>", "\line ")
   cTemp := StrTran(cTemp, "</p>", "\line ")
   cTemp := StrTran(cTemp, "<div>", "\line ")
   cTemp := StrTran(cTemp, "</div>", "\line ")
   cTemp := StrTran(cTemp, "<span>", "")
   cTemp := StrTran(cTemp, "</span>", "")
   cTemp := StrTran(cTemp, "<em>", "{\i ")   //Italic
   cTemp := StrTran(cTemp, "</em>", "\i0")
   cTemp := StrTran(cTemp, "<strong>", "{\b ")   //bold
   cTemp := StrTran(cTemp, "</strong>", "\b0")
   cTemp := StrTran(cTemp, "<sup>", "{\super ")       // Superscript
   cTemp := StrTran(cTemp, "</sup>", "\nosupersub")
   cTemp := StrTran(cTemp, "<sub>", "{\sub ")         // SubScript
   cTemp := StrTran(cTemp, "</sub>", "\nosupersub")

   // Costruisci la stringa RTF finale
   cRtf := "{\rtf1\ansi\deff0{\fonttbl\f0\fcharset0 Courier;}{\colortbl;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;}"
   cRtf += cTemp
   cRtf += "}"

   RETURN cRtf


FUNCTION TestRtfM(cRtf)
    local oWnd
   // logfile("silvio.txt", cRtf )
   DEFINE WINDOW ownd
   oWnd:bPainted := { || oWnd:SayText( cRtf ) }
   ACTIVATE WINDOW oWnd CENTERED

return nil
 


Run all but on Rtf the table is not created good,also the link tag not run ok
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: creating a new function HtmlToRtf()

Postby Otto » Thu Feb 15, 2024 11:13 pm

One could install pandoc and call it with shellexec("C:\html>pandoc tabelle.html -o tabelle.rtf"). Then, to the code you get in tabelle.rtf, insert your RTF header:
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{*\generator Riched20 10.0.19041}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang7 and then add } at the end. I made a test and it is working fine.

https://github.com/jgm/pandoc/releases/tag/3.1.12
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6312
Joined: Fri Oct 07, 2005 7:07 pm

Re: creating a new function HtmlToRtf()

Postby Silvio.Falconi » Fri Feb 16, 2024 11:31 am

Otto wrote:One could install pandoc and call it with shellexec("C:\html>pandoc tabelle.html -o tabelle.rtf"). Then, to the code you get in tabelle.rtf, insert your RTF header:
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{*\generator Riched20 10.0.19041}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang7 and then add } at the end. I made a test and it is working fine.

https://github.com/jgm/pandoc/releases/tag/3.1.12


what...? I do not understand ?
my HTMLTORTF function takes the generated cstring and converts it to rtf


I don't know how to convert the tag for tables and links
Code: Select all  Expand view
cTemp := StrTran(cTemp, "<table border='1'>", "\pard\intbl\itap0\plain\ltrpar{\trowd\trgaph70\trleft-70")
   cTemp := StrTran(cTemp, "<tr>", "\trbrdrt\brdrdb\cell ")
   cTemp := StrTran(cTemp, "</tr>", "\cell\cell\row}")
   cTemp := StrTran(cTemp, "<td>", "")
   cTemp := StrTran(cTemp, "</td>", "\cell")
   cTemp := StrTran(cTemp, "border='1'", "\brdrw10\brdrs")
   cTemp := StrTran(cTemp, "</table>", "\par}")

   // Aggiungi il supporto per i link
   cTemp := StrTran(cTemp, "<a href=", " {\field{\*\fldinst{HYPERLINK ")
   cTemp := StrTran(cTemp, "</a>", "}}{\fldrslt ")
   cTemp := StrTran(cTemp, ">", "")
 


these lines could be wrong, in fact when I load the rtf file you can't see anything
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: creating a new function HtmlToRtf()

Postby Silvio.Falconi » Fri Feb 16, 2024 2:30 pm

cTemp := StrTran(cTemp, "<a href=", "{\field{\*\fldinst{HYPERLINK ")
cTemp := StrTran(cTemp, "</a>", "}}{\fldrslt ")
cTemp := StrTran(cTemp, ">", "}}")


I see this but it is not clickable

Image
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 60 guests