Dynamically Adding Text and Images to Existing PDF Using PHP

For one of the projects I am working on, I need to issue a press pass for customers that place an order. The traditional method of doing this would be to open up Photoshop each time and swap out the content and export as a PDF but I wanted a more streamlined approach—this is where PHP becomes an valuable asset.

After doing some research, I was able to find two libraries—one that handled importing an existing PDF (FPDI Libary) and one that handled adding text and images to the PDF (FPDF Library). There are massive applications for dynamically adding text and images to a PDF. Some of the applications include generating custom invoices, letters, certificates, print and so much more!

Below is a screenshot of the original template and final output for what I was working on:


For both security and practical purposes, I cleaned up my code and removed the excess that was applicable to just my project. Below is the clean version of my PHP code:


function generatePDF($source, $output, $text, $image) {

$pdf = new FPDI('Portrait','mm',array(215.9,279.4)); // Array sets the X, Y dimensions in mm
$pagecount = $pdf->setSourceFile($source);
$tppl = $pdf->importPage(1);

$pdf->useTemplate($tppl, 0, 0, 0, 0);

$pdf->Image($image,10,10,50,50); // X start, Y start, X width, Y width in mm

$pdf->SetFont('Helvetica','',10); // Font Name, Font Style (eg. 'B' for Bold), Font Size
$pdf->SetTextColor(0,0,0); // RGB 
$pdf->SetXY(51.5, 57); // X start, Y start in mm
$pdf->Write(0, $text);

$pdf->Output($output, "F");

generatePDF("template.pdf", "export.pdf", "Hello world", "image.jpg");

Installing Webuzo Control Panel on Linux

Webuzo is a web hosting control panel for Linux. The few things that set Webuzo apart from other LAMP (Linux Apache MySQL and PHP) stack web hosting control panels is that it is a single user control panel, easy to install but most notably is that it is free. They do have a paid version, and the difference between the free and paid version is the number of scripts available—55 scripts and 391 scripts, respectively. These scripts allow you to install web applications with 1 click and integrates fully into the Webuzo control panel framework.

You can install Webuzo on any of the popular Linux distributions. According to their system requirements, it works for 32-bit (x86) but the installer gave me some issues so I installed it on 64-bit (x86_64). The indicated minimum requirement for RAM is 512mb but it does work on 256mb of RAM.

For this tutorial, I am using CentOS 7 64-bit so if you are using Debian, you will need to use the apt-get counterpart instead of yum.

Webuzo Screenshot

Continue reading “Installing Webuzo Control Panel on Linux”

SOCKS Proxy using SSH Tunneling

SOCKS or Socket Secure is an protocol used to move network packets between the computer and the proxy server. You can use any standard Linux server to set up your SOCKS proxy as long as you have SSH (Secure Shell) access. You do not need to install any additional software on your server for this to work.

As a digital marketer doing SEO, you may need to run SERP rank checks and if you do it often enough, search engines may temporarily block your computer’s public IP address. Or if you are at work or school where IT blocks websites that you need to access, this can be a viable method to get around that—as long as you are not doing anything illegal.

Here are some advantages of SOCKS proxies over other types of proxies:

  • Lives on the Session Layer (refer to the OSI model) so anything above that on the Presentation Layer and Application Layer (eg. HTTP, FTP, etc.) will all pass through the SOCKS proxy
  • Use a proxy on SEO software that does not have native support for  proxies
  • Does not require anything special as long as you have a Linux server with SSH access
  • Avoid the risk of middlemen stealing your data in contrast to public proxies

Note: Even though DNS is on the Application Layer, Windows will use your default DNS, in which case would probably be from your ISP. You can use a software such as DNS2SOCKS if you want to route DNS traffic through the SOCKS proxy too.

This tutorial is for Windows and you would need to download PuTTY (SSH client) if you do not have it.

Download PuTTY

Continue reading “SOCKS Proxy using SSH Tunneling”

Throttling CPU Usage for Applications (Windows)

This is for CPU throttling on the application level, and is different than dynamic frequency scaling, which is used to prevent overheating and save power. CPU throttling for applications allows you to control how much overall CPU usage a specific application is allowed to use.

Sometimes applications can go rampant on CPU consumption, which may cause other applications or even the operating system to become unstable. This can happen if the application was designed for a smaller use case and you are pushing the application’s boundaries–for example, a CSV editing application may be designed and tested for up to thousands of records but if you open a CSV file with 10 million records, it max out your computer’s CPU cycles.

I recommend using a software called BES, which stands for Battle Encoder Shirase (no idea what this means). It is a simple piece of software that allows you to Target and Limit the CPU usage by percentage for up to 3 applications.

Download BES

The website says it is compatible with Windows 7/XP/2000 but if you have Windows 8/ 8.1/10, you can still use it without any issues. Below is a screenshot of the software—not the prettiest user interface but it gets the job done.

Removing from DNSBL or RBL Blacklists

DNSBL or DNS Blackhole List and RBL or Realtime Blackhole List are great for the consumer but for businesses and marketers, it can be a huge headache—even if the email sending is legitimate and opt-in because they are going by IP reputation and domain name reputation (reverse IP). If you have a large mailing list (opted in) that has not seen the light of day for some time, this can trigger these DNSBL / RBL services such as Barracuda and Spamhaus.

What is DNSBL / RBL?

This is an email spam prevention effort that flags locations on the Internet that has a reputation of spamming. There are tons of providers but some of the more popular ones include Barracuda, Spamhaus, Protected Sky RBL and Invaluement. These DNSBL services create blacklists and the recipient ISPs will check with one or multiple DNSBL providers to see if the email will make it to the Inbox or not. If it is flagged as spam, it will enter a “black hole” and will not make it to the recipients Inbox or even Spam box. That’s correct. If it is flagged with a DNSBL provider, it may not even show up in the Spam box.

How to check if my domain or IP is blacklisted?

I recommend using MX Toolbox and the Multi RBL checker by valli.org.

DNSBL and RBL Protection and White Listing

How do I remove myself from these blacklists?

There are tons of DNSBL / RBL providers and each of them has their own process to remove from their blacklist. I have included the links to remove from blacklist for some of the popular DNSBL / RBL providers that tend to be on the aggressive side with blacklisting.

Continue reading “Removing from DNSBL or RBL Blacklists”

Bulk Linux Server Management Automation

One of the problems that techies who like to get their hands dirty with the nitty gritty of their server configuration and management is scaling the management of their Linux dedicated servers or VPSes (virtual private servers). To clarify, I am not referring to basic server management that comes included with most higher end server packages—I am referring to automation services that need to be done on a regular basis.

As an example, if you have 20 servers and on a regular basis, you need to bulk restart Apache, Exim, MySQL or the servers altogether, this method would save a ton of time in contrast to logging into each server individually using SSH and running these commands.

Here is how you would do it:

  1. Make sure you have PuTTY, and you can download it here (download putty.exe).
  2. In the same folder that you have putty.exe, create a .bat text file using Notepad in this case I am calling it servers.bat and create a .txt file using Notepad in this case I am calling it server_exec.txt
  3. Inside servers.bat, you want to put putty.exe root@server.com -pw password -m servers_exec.txt and repeat each server on a new line.
  4. Inside server_exec.txt, you want to put the SSH commands, one per line.
  5. Now, double click on servers.bat and it’ll login to each server and run your commands.

This concept can be taken and expanded on for more complex automation. For example, if each of your servers requires its own set of commands, you can create a separate .txt file for each server and update it in the corresponding line in your .bat file.

Importing Large Data Set (2GB CSV File) Into MySQL Database

As a digital marketer, sometimes you have to deal with large data sets—large enough that Microsoft Excel will be unable to open it. Microsoft Excel has a tendency of truncating data once it reaches a million records. In order to manipulate the data, I imported the CSV file into a MySQL database table so I can run SQL queries to pull the data that I need. The challenge comes with importing the 2GB CSV data because the web based MySQL client phpMyAdmin or even desktop MySQL clients will not be able to do the import without crashing.

What I did was upload the CSV file to the web server and used MySQL’s LOAD DATA command to do the import. The CSV file was 2GB with 9.2 million records and upon importing it, it took up 1.4GB of space on the database server.

LOAD DATA LOCAL INFILE '/home/user/dataset.csv' INTO TABLE `tablename` FIELDS TERMINATED BY ',' (col1, col2, col3, col4, col5);

Simply replace the CSV file path, table name and column IDs.

If you need to remove all of the duplicate entries, here is the SQL query to do so:

DELETE FROM tablename 
             FROM (SELECT id FROM tablename 
                   GROUP BY col1, col2, col3 HAVING (COUNT(*) > 1)
                  ) AS A

Simply replace the table name and column IDs.

And, if you need to do a bulk find and replace, here is the SQL query:

UPDATE us_emails SET col1 = REPLACE(col1,'find_this','replace_with_this');

Lastly, if you need to export the MySQL database tables as CSV files, you can do so in phpMyAdmin but for really large files it may time out. In this case, you would need to use SSH and use the following command:

mysql -u root -ppassword database_name -e "SELECT * INTO OUTFILE '/full_server_path/filename.csv'  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'  LINES TERMINATED BY '\r\n' FROM table_name;"

Simply replace the root username, password, database name, output file full path and file name, and table name.

Below is a screenshot of the MySQL database:

Importing 2GB CSV File into MysQL Database

Download Full List of United States Cities CSV

As a digital marketer, especially for businesses that provide location based services that span across the United States, sometimes it can be an annoyance finding a list of every United States based city. Fortunately, Google has thought ahead and provides all this information and it spans across the globe so it is not restricted to just the United States.

In Google Developers’s Reference Data, there is a section called Geographical Targets that allows you to specify the Country Code and Target Type. In my scenario, the Country Code would be US and the Target Type would be City, but you can filter this out to provide data such as Airports, County, Districts, Regions, Universities and much more!

Once you have set your Geographical Target, you can download a CSV file and import it into Excel or Google Sheets for further data manipulation and filtering. For those who just want to download the CSV without using Google Developers, I have provided the CSV download below.

Download Full U.S. Cities CSV

Full List of United States Cities CSV

Regex or Regular Expressions in Google Analytics Custom Reports

Regular expressions, or regex for short, is a string of text used to describe a search pattern. In simple terms, it is find and replace on steroids. In this specific example, we are not replacing anything. There are a ton of applications ranging from parsing data on a web server to manipulating high volumes of text files to what makes SEO permalinks possible on Linux servers, which is done using Apache’s mod_rewrite by editing the .htaccess file. In Google Analytics, you can create Custom Reports using regular expressions to build customized reporting views that allow you to get to the data that you want in the format that you want it presented. I will show you how.

To keep things simple, I will use the example that I want to build a Custom Report to see the traffic coming from Facebook. Traffic from Facebook generally comes in from facebook.com, m.facebook.com, l.facebook.com and lm.facebook.com. The l.* and lm.* are part of their Link Shim release in 2008, which helps protect Facebook users from malicious URLs.

Custom Reports Basics
To create a Custom Report, click on the Customization tab on the top and then click the “New Custom Report” button.

I gave mine the title of “Facebook Traffic” and left the report Name the default. Under Metric Groups, I added “Users”, “New Users”, “Sessions”, “% New Sessions”, “Pageviews” and “Avg. Session Duration”. Under Dimension Drilldowns, I added “Source / Medium”, “Full Referrer” and “Landing Page”. You can change the metrics and dimensions to the ones that you want to see but for this purpose, you can use the ones that I have used to make it easier on your end. If you click on Save, you will see all traffic—I will show you below several methods to filter out just traffic from Facebook.

Google Analytics Custom Reports

Go back to Customization, and under the Actions dropdown, click on Edit. Under “Filters – optional”, click on “add filter” and select Source (under Acquisitions).
Continue reading “Regex or Regular Expressions in Google Analytics Custom Reports”

PHP Array vs. MySQL Query Performance Test

Why is this important in relation to digital marketing? Google. Along with the Google mobile friendly algorithm update, the search engine power house also attributes page speed with SERP (search engine results page) rankings. This is relevant to developers, entrepreneurs and business executives who have websites containing a lot of data.

What is a PHP array?
In simple terms, an array is a list, which can be a linear list or a multi-dimensional list. The data from a SQL database can be stored into an array to be interpreted and manipulated by PHP, which lives in the RAM or memory of the server.

What is a MySQL query?
A MySQL database holds information stored within tables with columns and rows as would in an Excel document, and these tables can be “queried” for the specific data arranged in the manner that you want.

In this specific case scenario, I have a database table of contacts with 26 columns and 100,000+ rows and I need to display the total number of contacts per unique company for each state in an HTML table. The first approach would be the easier approach of looping through each state and querying the total from the database, resulting in an output of 1 row per request across 50 requests. The second approach would be to load all 50 rows into a PHP array, resulting in an output of 50 rows per request across 1 request, and then looping through the array to search for the matching state, 50 times.

Initial Run

The PHP array approach completely annihilated the MySQL query approach. Pingdom’s page speed test showed that the PHP array script executed in 1.28 seconds while the MySQL query script took 56.76 seconds to execute. Both approaches return the exact same resulting HTML.

pagespeed-initial-sql pagespeed-initial-php
Continue reading “PHP Array vs. MySQL Query Performance Test”