Bom dia Pessoal !
Estou com um problema aqui em que estou usando um código para conversão de RTF para HTML e localmente usando XAMPP e o PHP no vscode funcionou corretamente, mas quando passo o mesmo código para o ScriptCase gera erro, pensei por ter coisas do tipo ‘//’ em string, mas não consegui uma solução. Se alguém puder dar uma ajuda, agradeço desde já!, segue abaixo os testes e o código utilizado.
Segue o código (utilizei o mesmo no php com um exemplo funcionando no xampp, usei o mesmo no scriptCase em uma Blank)
Desculpa pelo tamanho grande do código, tentei pegar algo que sem mt require e dependencias.
<?php
/**
* Exemplo de uso:
* $reader = new RtfReader();
* $rtf = file_get_contents("test.rtf"); // or use a string
* $reader->Parse($rtf);
* //$reader->root->dump(); // to see what the reader read
* $formatter = new RtfHtml();
* echo $formatter->Format($reader->root);
*/
class RtfElement
{
protected function Indent($level)
{
for($i = 0; $i < $level * 2; $i++) echo " ";
}
}
class RtfGroup extends RtfElement
{
public $parent;
public $children;
public function __construct()
{
$this->parent = null;
$this->children = array();
}
public function GetType()
{
// No children?
if(sizeof($this->children) == 0) return null;
// First child not a control word?
$child = $this->children[0];
if(get_class($child) != "RtfControlWord") return null;
return $child->word;
}
public function IsDestination()
{
// No children?
if(sizeof($this->children) == 0) return null;
// First child not a control symbol?
$child = $this->children[0];
if(get_class($child) != "RtfControlSymbol") return null;
return $child->symbol == '*';
}
public function dump($level = 0)
{
echo "<div>";
$this->Indent($level);
echo "{";
echo "</div>";
foreach($this->children as $child)
{
if(get_class($child) == "RtfGroup")
{
if ($child->GetType() == "fonttbl") continue;
if ($child->GetType() == "colortbl") continue;
if ($child->GetType() == "stylesheet") continue;
if ($child->GetType() == "info") continue;
// Skip any pictures:
if (substr($child->GetType(), 0, 4) == "pict") continue;
if ($child->IsDestination()) continue;
}
$child->dump($level + 2);
}
echo "<div>";
$this->Indent($level);
echo "}";
echo "</div>";
}
}
class RtfControlWord extends RtfElement
{
public $word;
public $parameter;
public function dump($level)
{
echo "<div style='color:green'>";
$this->Indent($level);
echo "WORD {$this->word} ({$this->parameter})";
echo "</div>";
}
}
class RtfControlSymbol extends RtfElement
{
public $symbol;
public $parameter = 0;
public function dump($level)
{
echo "<div style='color:blue'>";
$this->Indent($level);
echo "SYMBOL {$this->symbol} ({$this->parameter})";
echo "</div>";
}
}
class RtfText extends RtfElement
{
public $text;
public function dump($level)
{
echo "<div style='color:red'>";
$this->Indent($level);
echo "TEXT {$this->text}";
echo "</div>";
}
}
class RtfReader
{
public $root = null;
protected function GetChar()
{
$this->char = $this->rtf[$this->pos++];
}
protected function ParseStartGroup()
{
// Store state of document on stack.
$group = new RtfGroup();
if($this->group != null) $group->parent = $this->group;
if($this->root == null)
{
$this->group = $group;
$this->root = $group;
}
else
{
array_push($this->group->children, $group);
$this->group = $group;
}
}
protected function is_letter()
{
if(ord($this->char) >= 65 && ord($this->char) <= 90) return TRUE;
if(ord($this->char) >= 97 && ord($this->char) <= 122) return TRUE;
return FALSE;
}
protected function is_digit()
{
if(ord($this->char) >= 48 && ord($this->char) <= 57) return TRUE;
return FALSE;
}
protected function ParseEndGroup()
{
// Retrieve state of document from stack.
$this->group = $this->group->parent;
}
protected function ParseControlWord()
{
$this->GetChar();
$word = "";
while($this->is_letter())
{
$word .= $this->char;
$this->GetChar();
}
// Read parameter (if any) consisting of digits.
// Paramater may be negative.
$parameter = null;
$negative = false;
if($this->char == '-')
{
$this->GetChar();
$negative = true;
}
while($this->is_digit())
{
if($parameter == null) $parameter = 0;
$parameter = $parameter * 10 + $this->char;
$this->GetChar();
}
if($parameter === null) $parameter = 1;
if($negative) $parameter = -$parameter;
// If this is \u, then the parameter will be followed by
// a character.
if($word == "u")
{
}
// If the current character is a space, then
// it is a delimiter. It is consumed.
// If it's not a space, then it's part of the next
// item in the text, so put the character back.
else
{
if($this->char != ' ') $this->pos--;
}
$rtfword = new RtfControlWord();
$rtfword->word = $word;
$rtfword->parameter = $parameter;
array_push($this->group->children, $rtfword);
}
protected function ParseControlSymbol()
{
// Read symbol (one character only).
$this->GetChar();
$symbol = $this->char;
// Symbols ordinarily have no parameter. However,
// if this is \', then it is followed by a 2-digit hex-code:
$parameter = 0;
if($symbol == '\'')
{
$this->GetChar();
$parameter = $this->char;
$this->GetChar();
$parameter = hexdec($parameter . $this->char);
}
$rtfsymbol = new RtfControlSymbol();
$rtfsymbol->symbol = $symbol;
$rtfsymbol->parameter = $parameter;
array_push($this->group->children, $rtfsymbol);
}
protected function ParseControl()
{
// Beginning of an RTF control word or control symbol.
// Look ahead by one character to see if it starts with
// a letter (control world) or another symbol (control symbol):
$this->GetChar();
$this->pos--;
if($this->is_letter())
$this->ParseControlWord();
else
$this->ParseControlSymbol();
}
protected function ParseText()
{
// Parse plain text up to backslash or brace,
// unless escaped.
$text = "";
do
{
$terminate = false;
$escape = false;
// Is this an escape?
if($this->char == '\\')
{
// Perform lookahead to see if this
// is really an escape sequence.
$this->GetChar();
switch($this->char)
{
case '\\': $text .= '\\'; break;
case '{': $text .= '{'; break;
case '}': $text .= '}'; break;
default:
// Not an escape. Roll back.
$this->pos = $this->pos - 2;
$terminate = true;
break;
}
}
else if($this->char == '{' || $this->char == '}')
{
$this->pos--;
$terminate = true;
}
if(!$terminate && !$escape)
{
$text .= $this->char;
$this->GetChar();
}
}
while(!$terminate && $this->pos < $this->len);
$rtftext = new RtfText();
$rtftext->text = $text;
array_push($this->group->children, $rtftext);
}
public function Parse($rtf)
{
$this->rtf = $rtf;
$this->pos = 0;
$this->len = strlen($this->rtf);
$this->group = null;
$this->root = null;
while($this->pos < $this->len)
{
// Read next character:
$this->GetChar();
// Ignore \r and \n
if($this->char == "\n" || $this->char == "\r") continue;
// What type of character is this?
switch($this->char)
{
case '{':
$this->ParseStartGroup();
break;
case '}':
$this->ParseEndGroup();
break;
case '\\':
$this->ParseControl();
break;
default:
$this->ParseText();
break;
}
}
}
}
class RtfState
{
public function __construct()
{
$this->Reset();
}
public function Reset()
{
$this->bold = false;
$this->italic = false;
$this->underline = false;
$this->end_underline = false;
$this->strike = false;
$this->hidden = false;
$this->fontsize = 0;
}
}
class RtfHtml
{
public function Format($root)
{
$this->output = "";
// Create a stack of states:
$this->states = array();
// Put an initial standard state onto the stack:
$this->state = new RtfState();
array_push($this->states, $this->state);
$this->FormatGroup($root);
return $this->output;
}
protected function FormatGroup($group)
{
// Can we ignore this group?
if ($group->GetType() == "fonttbl") return;
if ($group->GetType() == "colortbl") return;
if ($group->GetType() == "stylesheet") return;
if ($group->GetType() == "info") return;
// Skip any pictures:
if (substr($group->GetType(), 0, 4) == "pict") return;
if ($group->IsDestination()) return;
// Push a new state onto the stack:
$this->state = clone $this->state;
array_push($this->states, $this->state);
foreach($group->children as $child)
{
if(get_class($child) == "RtfGroup") $this->FormatGroup($child);
if(get_class($child) == "RtfControlWord") $this->FormatControlWord($child);
if(get_class($child) == "RtfControlSymbol") $this->FormatControlSymbol($child);
if(get_class($child) == "RtfText") $this->FormatText($child);
}
// Pop state from stack.
array_pop($this->states);
$this->state = $this->states[sizeof($this->states)-1];
}
protected function FormatControlWord($word)
{
if($word->word == "plain") $this->state->Reset();
if($word->word == "b") $this->state->bold = $word->parameter;
if($word->word == "i") $this->state->italic = $word->parameter;
if($word->word == "ul") $this->state->underline = $word->parameter;
if($word->word == "ulnone") $this->state->end_underline = $word->parameter;
if($word->word == "strike") $this->state->strike = $word->parameter;
if($word->word == "v") $this->state->hidden = $word->parameter;
if($word->word == "fs") $this->state->fontsize = ceil(($word->parameter / 24) * 16);
if($word->word == "par") $this->output .= "<p>";
// Characters:
if($word->word == "lquote") $this->output .= "‘";
if($word->word == "rquote") $this->output .= "’";
if($word->word == "ldblquote") $this->output .= "“";
if($word->word == "rdblquote") $this->output .= "”";
if($word->word == "emdash") $this->output .= "—";
if($word->word == "endash") $this->output .= "–";
if($word->word == "bullet") $this->output .= "•";
if($word->word == "u") $this->output .= "◊";
}
protected function BeginState()
{
$span = "";
if($this->state->bold) $span .= "font-weight:bold;";
if($this->state->italic) $span .= "font-style:italic;";
if($this->state->underline) $span .= "text-decoration:underline;";
if($this->state->end_underline) $span .= "text-decoration:none;";
if($this->state->strike) $span .= "text-decoration:strikethrough;";
if($this->state->hidden) $span .= "display:none;";
if($this->state->fontsize != 0) $span .= "font-size: {$this->state->fontsize}px;";
$this->output .= "<span style='{$span}'>";
}
protected function EndState()
{
$this->output .= "</span>";
}
protected function FormatControlSymbol($symbol)
{
if($symbol->symbol == '\'')
{
$this->BeginState();
$this->output .= htmlentities(chr($symbol->parameter), ENT_QUOTES, 'ISO-8859-1');
$this->EndState();
}
}
protected function FormatText($text)
{
$this->BeginState();
$this->output .= $text->text;
$this->EndState();
}
}
$reader = new RtfReader();
//$rtf = file_get_contents("plstemp.rtf"); // or use a string
$rtf = "{\\rtf1\ansi\deff0
{\\fonttbl{\\f0\\fnil Courier New;}}
{\colortbl ;\\red255\green255\blue255;\\red204\green204\blue204;\\red51\green51\blue51;}
{\stylesheet{\s6\sbasedon2\snext6 default;}{\s24\sbasedon2\snext24 default;}{\s7\sbasedon2\snext7 default;}{\s27\sbasedon2\snext27 default;}{\s16\sbasedon2\snext16 default;}{\s13\sbasedon2\snext13 default;}{\s21\sbasedon2\snext21 default;}{\s9\sbasedon2\snext9 default;}{\s26\sbasedon2\snext26 default;}{\s3\sbasedon2\snext3 default;}{\s14\sbasedon2\snext14 default;}{\s15\sbasedon2\snext15 default;}{\s23\sbasedon2\snext23 default;}{\s10\sbasedon2\snext10 default;}{\s11\sbasedon2\snext11 default;}{\s20\sbasedon2\snext20 default;}{\s19\sbasedon2\snext19 default;}{\s12\sbasedon2\snext12 default;}{\s18\sbasedon2\snext18 default;}{\s25\sbasedon2\snext25 default;}{\s5\sbasedon2\snext5 default;}{\s2 default;}{\s22\sbasedon2\snext22 default;}{\s8\sbasedon2\snext8 default;}{\s17\sbasedon2\snext17 default;}{\s1\sbasedon2\snext1 default;}{\s4\sbasedon2\snext4 default;}}
\s1\ql\\f0\\fs20\i0\b\ul0\highlight1\cf0\par
\ql\highlight2 HEMOGRAMA COMPLETO \highlight1 \\fs22 \\fs20\par
\ql\\fs22 \b0 \b\par
\ql\\fs18\par
\ql\\fs20\highlight2 ERITROGRAMA \highlight1\par
\ql\\fs18 Valores encontrados \b0 \b Valores de refer\'eancia\par
\ql\\fs10\b0\par
\pard\s3\ql\\fs18\highlight2 Hemacias............: \b 3,79 milh\'f5es/\b0 3,80 - 4,80 \highlight1\par
\pard\s4\ql Hemoglobina.........: \b 11,3 g/dl \b0 12,0 - 15,0 \par
\pard\s5\ql\highlight2 Hematocrito.........: \b 33,0 % \b0 36 - 46 \highlight1\par
\pard\s6\ql Vol. Glob. Medio....: \b 87,0 fl \b0 83 - 101\par
\pard\s7\ql\highlight2 Hem. Glob. Medio....: \b 29,8 pg \b0 27 - 32 \highlight1\par
\pard\s8\ql C.H. Glob. Media....: \b 34,1 % \b0 31,5 - 34,5\par
\pard\s9\ql\highlight2 RDW.................: \b 12,3 % \b0 11,8 - 15,6 \highlight1\par
\pard\s10\ql Observa\'e7\'e3o:Hem\'e1cias normoc\'edticas e com satura\'e7\'e3o hemoglob\'ednica normal \par
\ql\par
\ql\\fs20\b\highlight2 LEUCOGRAMA \highlight1\par
\ql\\fs18 Valores encontrados Valores de refer\'eancia\par
\ql\b0 % mm\'b3 % mm\'b3\par
\pard\s11\ql\highlight2 Leucocitos..........: \b 100 3.900\b0 4.000 - 10.000 \highlight1 \par
\pard\s12\ql Neutr\'f3filos.........: \b 49 1.911\b0 40 a 75 1.700 - 7.000\par
\pard\s13\ql\highlight2 Promielocitos.......: \b 0 0\b0 0 0 \highlight1\par
\pard\s14\ql Mielocitos..........: \b 0 0\b0 0 0\par
\pard\s15\ql\highlight2 Metamielocitos......: \b 0 0\b0 0 - 1 0 \highlight1\par
\pard\s16\ql Bastonetes..........: \b 0 0\b0 1 - 3 At\'e9 840\par
\pard\s17\ql\highlight2 Segmentados.........: \b 49 1.911\b0 40 - 75 1.700 - 7.000 \highlight1\par
\pard\s18\ql Eosinofilos.........: \b 9 351\b0 1 - 6 50 - 500\par
\pard\s19\ql\highlight2 Basofilos...........: \b 0 0\b0 0 - 1 0 - 300 \highlight1\par
\pard\s20\ql Linfocitos t\'edpicos..: \b 33 1.287\b0 20 - 45 1000 - 4800 \par
\pard\s21\ql\highlight2 Linfocitos at\'edpicos.: \b 0 0 \b0 Inferior a 10 % dos linf\'f3citos t\'edpicos \highlight1\par
\pard\s22\ql Mon\'f3citos...........: \b 9 351\b0 2 - 10 300 - 900\par
\pard\s23\ql\highlight2 Observa\'e7\'e3o: Leuc\'f3citos sem altera\'e7\'f5es degenerativas \highlight1\par
\ql\par
\ql\\fs20\b\highlight2 PLAQUETAS \cf3 \highlight1\par
\ql\\fs18\cf0 Valor encontrado Valor de refer\'eancia\par
\pard\s24\ql\b0\highlight2 Contagem de Plaquetas: 185.000 mm\'b3 150.000 - 450.000 mm\'b3 \highlight1\par
\pard\s25\ql Observa\'e7\'e3o: Plaquetas morfo e numericamente normais \par
\ql \par
\pard\s26\ql\\fs16 M\'e9todo: Automa\'e7\'e3o Pentra 80 e Microscopia \par
\pard\s27\ql Tipo de Amostra: Sangue total com EDTA Data da Coleta: 28/01/2022 Hora:07:22\par
\ql\\fs22\i0\b0\ul0\highlight0\par
}
"; // or use a string
$reader->Parse($rtf);
$reader->root->dump(); // to see what the reader read
$formatter = new RtfHtml();
//echo $formatter->Format($reader->root);
$resultado = $formatter->Format($reader->root);
echo $resultado;
?>