I'm using the GD Library to create images from data I'm pulling from an API.
The strings that are returned can sometimes be kind of lengthy, and I'm hoping to find a way to automatically create a new line for text if the string goes too far.
Is there something like this built into the GD library, or will I have to write some code to count the characters and move everything to a new line if it goes too long?
GD is strictly for drawing. You'll need a text layout engine such as Pango.
I am not familier with a built-in function that automatically creates new lines,
so I guess you need to write a php function that sorts the string to "sub-strings"
according to your width length and then use them in your image.
Consider looking at this post:
http://www.php.net/manual/en/function.imagestring.php#90481
Related
Is this possible? or must one use a PHP library such as thephpleague/csv?
Example, according to PhpSpreadsheet's documentation:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load($file);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->setUseBOM(true);
$writer->setDelimiter(',');
$writer->setEnclosure('');
$writer->setLineEnding("\r\n");
$writer->setSheetIndex(0);
$writer->save('test.csv');
As you can see this completely counter-productive as the load() method requires an actual $file to do essentially the same thing.
Is there a way to use PhpOffice\PhpSpreadsheet\Writer\Csv() without using the spreadsheet and instead using a string containing CSV data?
Thanks in advance for all positive inputs and suggestions.
You don't need to load a file, you can create an empty Spreadsheet object directly by using
$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
as shown in the "Hello World" example in the PHPSPreadsheet documentation
But if all you want to do is write a csv file; then you're far better using PHP's built-in fputcsv() function than a library designed to manipulate genuine spreadsheets with multiple worksheets, formatting, formulae, and working with multiple different file formats, etc.
Or for simply writing a string that's already concatenated (are you sure that you've quoted everything that needs quoting, and escaped everything that needs escaping), just use fwrite().
Counter-productive is building your own csv string and then using a spreadsheet library to write each line.
I have a pdf document and I want to check if a specific text occurs (which are tags that I put in while generating the pdf) in the document, however using these libraries (tcpdfFpdi, pdftk or fdpi) I couldn't figure out if it's possible or how to do it.
$str = "{hello}";
$pdf = new TcpdfFpdi();
$pdf->setSourceFile($filePath);
$pdf->searchForText($str); // something like this which returns boolean
If I try without any library to dd(file_get_contents($filePath)), it returns a very long output and doesn't seem to contain the file I want so I think it's better to use one of those libraries.
Just an idea…
It's no actual PHP solution but you could use tools like pdftotext which I know from this post (where a PDF file is converted into a string to count its words): https://superuser.com/a/221367/535203
You can install it and play around with that command and call it from within your PHP application.
As far as I remember (long time ago since I used pdftotext) the output text is not exaclty the PDF's content but to search a few tags in it it's at least a good try.
I'm looking for an free php library that can generate code diff HTML. Basically just like GitHub's code diffs pages.
I've been searching all around and can't find anything. Does anyone know of anything out there that does what I'm looking for?
It looks like I found what I'm looking for after doing more Google searches with different wording.
php-diff seems to do exactly what I want. Just a php function that accepts two strings and generates all the HTML do display the diff in a web page.
To add my two cents here...
Unfortunately, there are no really good diff libraries for displaying/generating diffs in PHP. That said, I recently did find a circuitous way to do this using PHP. The solution involved:
A pure JavaScript approach for rendering the Diff
Shelling out to git with PHP to generate the Diff to render
First, there is an excellent JavaScript library for rendering GitHub-style diffs called diff2html. This renders diffs very cleanly and with modern styling. However diff2html requires a true git diff to render as it is intended to literally render git diffs--just like GitHub.
If we let diff2html handle the rendering of the diff, then all we have left to do is create the git diff to have it render.
To do that in PHP, you can shell out to the local git binary running on the server. You can use git to calculate a diff on two arbitrary files using the --no-index option. You can also specify how many lines before/after the found diffs to return with the -U option.
On the server it would look something like this:
// File names to save data to diff in
$leftFile = '/tmp/fileA.txt';
$rightFile = '/tmp/fileB.txt';
file_put_contents($leftFile, $leftData);
file_put_contents($rightFile, $rightData);
// Generate git diff and save shell output
$diff = shell_exec("git diff -U1000 --no-index $leftFile $rightFile");
// Strip off first line of output
$diff = substr($diff, strpos($diff, "\n"));
// Delete the files we just created
unlink($leftFile);
unlink($rightFile);
Then you need to get $diff back to the front-end. You should review the docs for diff2html but the end result will look something like this in JavaScript (assuming you pass $diff as diffString):
function renderDiff(el, diffString) {
var diff2htmlUi = new Diff2HtmlUI({diff: diffString});
diff2htmlUi.draw(el);
}
I think what you're looking for is xdiff.
xdiff extension enables you to create and apply patch files containing differences between different revisions of files.
This extension supports two modes of operation - on strings and on files, as well as two different patch formats - unified and binary. Unified patches are excellent for text files as they are human-readable and easy to review. For binary files like archives or images, binary patches will be adequate choice as they are binary safe and handle non-printable characters well.
Is there a way to get PHP imagettftext() font height?
I know that we can get width by imagettfbbox(). Can we get the height too?
Yes, you evidently can simply be looking at the documentation. Scroll down and use your browsers find function to search for the following:
jodybrabec at gmail dot com
Below his name he provides a nice cusotm function for you to use.
(got a link actually right here, no need to use the find function: http://php.net/manual/en/function.imagettfbbox.php#105593)
have anyone come across a php code that convert text or doc into pdf ?
it has to follow the same format as the original txt or doc file meaning the line feed as well as new paragraph...
Converting from DOC to PDF is possible using phpLiveDocx:
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
$phpLiveDocx->setUsername('username')
->setPassword('password');
$phpLiveDocx->setLocalTemplate('document.doc');
// necessary as of LiveDocx 1.2
$phpLiveDocx->assign('dummyFieldName', 'dummyFieldValue');
$phpLiveDocx->createDocument();
$document = $phpLiveDocx->retrieveDocument('pdf');
file_put_contents('document.pdf', $document);
unset($phpLiveDocx);
For text to PDF, you can use the pdf extension is PHP.
You can view the examples here.
Have a look at this SO question. Using OpenOffice in command line mode for conversions can be done, though you'd have to search a bit for the conversion macro's. I'm not saying it's light-weight though :)
See HTML_ToPDF. It also works for text.
It has been a long time since I touched PHP, but if you can make web service calls from it then try this product. It provides excellent conversion fidelity. It also supports additional formats including Infopath, Excel, PowerPoint etc as well as Watermarking support.
Please note that I have worked on this product so the usual disclaimers apply.