bookmark_borderLibanon 2019 – Erinnerungen

Es war endlich soweit. Am 15.05.2019 bin ich nach 16 Jahren zum ersten Mal wieder in den Libanon geflogen. Es ging mir bei dieser Reise darum, die alten Orte, die in meiner Erinnerung eine große Rolle spielen, wieder zu besuchen.

Meine Reise in den Libanon war nur sehr kurz – ich hatte nur zwei komplette Tage und ein paar Stunden am Anreisetag zur Verfügung, so dass ich alle Orte, die ich besuchen wollte, bereits Wochen – oder sogar Monate – zuvor komplett durchgeplant hatte.

Dieser kleine Blogbeitrag soll in erster Linie dazu dienen, meine Erinnerungen für mich selbst festzuhalten. Wenn er darüber hinaus noch jemandem gefällt oder das Bild des Libanon im Kopf des Lesers etwas in ein positives Licht rückt – umso besser.

Continue reading “Libanon 2019 – Erinnerungen”

bookmark_borderPHP-Function: Create slugs from string

Every website needs to implement readable URLs for internal linking. When you are using a blogging tool like WordPress or a CMS like Typo3, you automatically get the URL slug generator built in.

But sometimes, you are developing your own software and are in need of a function that simply generates a slug for you.

Search no more, because here it is: the PHP function to generate slugs for you.

function slugify($input, $word_delimiter='-') {
	$slug = iconv('UTF-8', 'ASCII//TRANSLIT', $input);
	$slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $slug);
	$slug = strtolower(trim($slug, '-'));
	$slug = preg_replace("/[\/_|+ -]+/", $word_delimiter, $slug);
	return $slug;
}

$slugString = slugify("This is just a small test for a slug creation");
echo $slugString;
// returns : this-is-just-a-small-test-for-a-slug-creation

This PHP function takes a simple string as first parameter, which will be turned into your slug. In this example, we are using the string This is just a small test for a slug creation .

The function slugify  then returns a nice, readable slug for you.

this-is-just-a-small-test-for-a-slug-creation

If you want to use another delimiter, simply pass it as the second parameter to the function.

bookmark_borderjsPDF / html2pdf: add header and footer text to each page

In a recent project I needed to add header and footer text (like page count) to every single generated html2pdf / jsPDF page.

There are a lot of instructions on the web, but none of those are really doing what I wanted.

Basically, I needed to use the callback  function of html2pdf to loop over the total pages, and then access that single page to add text to it.

html2PDF: Add a header and footer to every page

So the first step is to use the callback  function of html2pdf :

html2pdf(content, {
    margin: 10,
    filename: "my.pdf",
    image: {type: 'jpeg', quality: 1},
    html2canvas: {dpi: 72, letterRendering: true},
    jsPDF: {unit: 'mm', format: 'a4', orientation: 'landscape'},
    pdfCallback: pdfCallback
})

And within the callback function, I am looping over the pages of the generated document. You can find the total number of pages within the returned jsPDF-Object  which is called pdfObject in my example.

function pdfCallback(pdfObject) {
    var number_of_pages = pdfObject.internal.getNumberOfPages()
    var pdf_pages = pdfObject.internal.pages
    var myFooter = "Footer info"
    for (var i = 1; i < pdf_pages.length; i++) {
        // We are telling our pdfObject that we are now working on this page
        pdfObject.setPage(i)
        // The 10,200 value is only for A4 landscape. You need to define your own for other page sizes
        pdfObject.text(myFooter, 10, 200)
    }
}

Using this code you can simply add a header and a footer text to each page of the generated PDF document.

To add a header text, you can simply add the following line to the above code.

pdfObject.text("my header text", 10, 10)

This will add the text “my header text” at the position x = 10, y = 10 to every page of your PDF document.