I have an HTML string I would like to convert to an RTF string. I am using PHP. Is this possible.
@jgibson You can use the code like the following to convert HTML string to RTF using .NET version of Aspose.Words:
string html = File.ReadAllText(@"C:\Temp\in.html");
// Load document from string.
using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
{
Document doc = new Document(htmlStream);
// Save document to Stream and convert to string.
using (MemoryStream rtfStream = new MemoryStream())
{
doc.Save(rtfStream, SaveFormat.Rtf);
string rtfString = Encoding.UTF8.GetString(rtfStream.ToArray());
}
}
Currently Aspose.Words is available for .NET, Java and Python. You can consider using cloud version of Aspose.Words to achieve the same in PHP:
https://docs.aspose.cloud/words/convert/
I saw that from another posting in the forum. I need the same option but using PHP instead of .NET.
Thank you for your assistance.
Sorry, just read the rest of the posting. If I cannot use the Aspose.Word solution to change a string to an RTF document, I will need to search out another solution.
@jgibson As an option you can consider using Aspose.Words for java via PHP/Java Bridge:
https://docs.aspose.com/words/java/php-and-aspose-words-for-java/
I am getting a SplFileObject with the name and path of a temporary file. However, I am at a loss on how to read the file. Can you elaborate?
Thanks.
Here is the solution to convert original document string (HTML) to a different format document string (RTF). Hope this helps someone in the future.
$wordsApi = new WordsApi('####-####-####-####-####', '####################');
$stream = fopen(__DIR__ . "\\" . $filename . ".html",'w');
if ($stream !== false) {
fwrite($stream, $html); //$html contains the HTML formatted string
fclose($stream);
$request = new ConvertDocumentRequest(
__DIR__ . "\\" . $filename . ".html",
"rtf",
NULL,
NULL,
NULL,
NULL
);
$response = $wordsApi->convertDocument($request);
$stream = fopen($response->getRealPath(),'r');
if ($stream !== false) {
$contents = stream_get_contents($stream);
fclose($stream);
}
//$contents has string containing file data in converted format
//(i.e. in this case RTF from HTML)
}
Cheers!
@jgibson Thank you for sharing your solution. It is perfect that you managed to achieve what you need. Please feel free to ask in case of any issues, we are always glad to help you.