Using PHP, creating hyperlinks on the screen is no problem using an ECHO statement.
Now that everything is working OK. I would like to save the output to a textfile in such a way that the hyperlinks still work when the file is opened in Word or excel.
You should have a look at the PHPWord library.
With this library you can create Word documents in PHP including hyperlinks and other awesome stuff.
Hyperlink example:
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add hyperlink elements
$section->addLink('http://www.google.com', 'Best search engine', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
$section->addTextBreak(2);
$PHPWord->addLinkStyle('myOwnLinkStyle', array('bold'=>true, 'color'=>'808000'));
$section->addLink('http://www.bing.com', null, 'myOwnLinkStyle');
$section->addLink('http://www.yahoo.com', null, 'myOwnLinkStyle');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Link.docx');
?>
Related
I know how to use the Google Translate API. I want to know one thing do you have an idea to translate the PDF file without losing in the file format. I had tried to convert the PDF file to DOCX and then translate the file and then return it to PDF but the conversion from PDF to DOCX failed because of many PHPWORD BUGs.
Here is the question and the codes that I had received in order to request the conversion from PDF to DOCX.
<?php
require_once 'vendor/autoload.php';
// Create a new PDF reader
$reader = \PhpOffice\PhpWord\IOFactory::createReader('PDF');
$reader->setReadDataOnly(true);
// Load the PDF file
$phpWord = $reader->load('example.pdf');
// Save the DOCX file
$writer = $PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
$writer->save('example.docx');
echo 'PDF file converted to DOCX successfully!';
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
// Convert PDF to text
exec('pdftotext -layout input.pdf output.txt');
// Load text file
$text = file_get_contents('output.txt');
// Create new DOCX file
$phpWord = new \PhpOffice\PhpWord();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText($text);
// Save DOCX file
$objWriter = WordIOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('output.docx');
Do you have any idea how I can translate the PDF file without going through the conversion to other formats?
I am trying to attach(embedding) a .docx file to word document using phpword addObject() function, it's attaching file but while clicking on attached file it's not opening. If i do it for .doc file it's opening the attached file. I am using phpword library.
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add text elements
$section->addText('You can open this OLE object by double clicking on the icon:');
$section->addTextBreak(2);
// Add object
$section->addObject('Test.docx');
//if i use $section->addObject('Test.doc'); it's opening attached file. here Test.doc is word97-2003 format.
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Object.docx');
?>
Multiple issues were already logged for this, but no solution proposed for the issue yet :-(
addObject() function is not working with .docx file, it's working with .doc(word97-2003) file
Add Object Issue
Unable to open embedded object
I have a FlipBook jquery page and too many ebooks(pdf format) to display on it. I need to keep these PDF's hidden so that I would like to get its content with PHP and display it with my FlipBook jquery page. (instead of giving whole pdf I would like to give it as parts).
Is there any way i can get whole content of PDF file with PHP?
I need to seperate them according to their pages.
You can use PDF Parser (PHP PDF Library) to extract each
and everything from PDF's.
PDF Parser Library Link: https://github.com/smalot/pdfparser
Online Demo Link: https://github.com/smalot/pdfparser/blob/master/doc/Usage.md
Documentation Link: https://github.com/smalot/pdfparser/tree/master/doc
Sample Code:
<?php
// Include Composer autoloader if not already done.
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
$text = $pdf->getText();
echo $text;
?>
Regarding another part of your Question:
How To Convert Your PDF Pages Into Images:
You need ImageMagick and GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The [0] means page 1.
I have an existing word document in my computer and like to edit this file from my website (using PHP). I was able to find PHPWORD but this deals with new documents only. I don't want to code PHP for the whole document, instead wish to use it for the stuff that varies.
Does anybody know any way out?
https://github.com/PHPOffice/PHPWord
PHPWord also features a Reader which can be used to edit existing documents.
#nssmart,
Yes,mark a section on your word document with a variable such as {value1} and then, if you have PHPWORD downloaded, you can replace that section using code
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(Doc location);
$document->setValue('value1', 'Description');
This can be used to fill data in tables also.
https://github.com/PHPOffice/PHPWord
I also did this problem in phpword using the following code
if(!file_exists('file/word.docx')){
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
$templateProcessor->setValue('name', 'Akbarali');
$templateProcessor->setValue('time','13.02.2021');
$templateProcessor->setValue('month', 'January');
$templateProcessor->setValue('state','Uzbekistan');
$templateProcessor->saveAs('file/word.docx');
}
This will change the words in the demo.docx file. The new file is then saved in the word.docx file folder.
you can define variables in the form of $ {name} in the demo word file.
that is, ${name}, ${time}, ${month} and ${state}
I have a word document with various sections.
I need to append some more text in a particular section of word document using PHP.
After searching in google, came across phpoffice/phpword.
Using that I am able to write a new word document.
Not getting how to insert text in a given section.
Looks there is template processing in phpoffice.
Using template processing is it possible to insert text in a given section.
Is there any such example ?
Or is there any other good open source (php based) available, that provides easy apis to append text in a particular section.
Using below code, I can generate a new word document in phpoffice.
$filename = "sample";
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText(htmlspecialchars("Some text"));
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('./doc/' . $filename . '.docx');
However I want to write section wise (with section numbers like 1.1 or 1.2).
At a later stage need to append some more text to a particular section.
If it can be done using phpoffice, that will be my most preferred approach.