Category: Linux

Generate PDF using python and HTML

Dependencies:

sudo pip3 install pdfkit
sudo apt-get install wkhtmltopdf

Pdfkit and wkhtmltopdf are the packages that you will be required before getting started.

PDF can be generated in 3 ways. One is using a website url, second one is using a html string and the last one is using a html file(optional css file can also be specified in all 3 ways).

In this article we will discuss the third way which is more advanced one and will be most useful for most of our requirements. Below is the program I am going to use to generate the PDF.

import pdfkit
import sys

inputfile = sys.argv[1]
outfile = args.argv[2]

options = {
      "enable-local-file-access": True,
      "enable /var/www/html/": True,
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
        ('cookie-empty-value', '""'),
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None
}
# Single CSS file
css = 'bootstrap.css'

#pdfkit.from_url('https://google.com', 'out1.pdf')
pdfkit.from_file(inputfile, outfile, options=options, css=css)
#pdfkit.from_string('Hello!', 'out3.pdf')

In the code we can notice at the bottom that we can use a URL, local html file or a plain html string. Also a bootstrap.css file is being used for our styles. Common options that can be used to configure the PDF can also be seen in the above code. For full list of options that can be used read the documentaion here.

Now this code takes two arguments first one is input html file and the other being name of the output pdf that you want to generate into.

More documentaion regarding WKHTMLTOPDF can be read here.

Python program for Generating pdf using html content

Set your tp link router as wifi booster

Connect to the router and open the url http://tplinklogin.net/

Enter username and password. Now navigate to Lan Settings under Network and change the IP address of the router that is different from the main router.

Save the changes, wait till the router restarts with new IP.

After the router restarts navigate to wireless security, click on enable WDS briddging. Select channel from main router. To get this information click survey, discover your main router and connect to it. Enter the password of the main router and while you are here note the channel of the main router and enter it in WDS bridging settings under label channel.

Router might restart here, then navigate to wireless security and set password for your 2nd router.

Also click on DHCP and disable DHCP.

Click reboot. Now you will notice that your second router acts as your wifi booster.

How to test PHP installation

To test a successful php installation. We can check the PHP version in your machine or test the entire PHP setup using following

open a file in which your web directory name it anything like php_test.php and write the following content.

?php
phpinfo();
?>

Save the file and open the file in your browser like localhost/php_test.php.

Then you will notice that if php is installed the following image will be displayed.

PHP successful installation screenshot

There is a video tutorial that explains the same as above.

Fix ubuntu freezning or hard disk switching to read mode due to bad sector or file system error

This is one of the most common problems that one might encounter these days. This can be due to hardware of software.

If the issue is because of software we can fix it using below mentioned ways..

First print out the output of the command dmesg and see if there are any errors printed in red color which does symbolize that you have a file system error.

Note: sda5 means the disk that you want to check for errors. You can see the name of your disk by using the command df -h

Install smartctl a tool to check your hard disk for errors.

After installing smartctl run the following command.

smartctl -t long /dev/sda5

Then carefully see the output of above command it displays the time it will take to complete the test you have just started. To view the test run the below command.

smartctl -l selftest /dev/sda5

Now run below commands to check the health of your hard disk.

smartctl -a /dev/sda5
smartctl -H /dev/sda5

You can also add a line to your grub file located at /etc/default/grub or modify the line if it already exists.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash libata.force=noncq"

This will make sure that while rebooting you run a force check for errors in your file system. Be careful because you might need to back up your data as you may lose some of it.

Another way to go into recovery mode is to press ESC while rebooting and choose recovery mode. This will make sure to fix file system errors also you can choose to run fsck/resume normal boot in the options provided.

How to use the command CD in Linux Terminal – Ubuntu, Fedora, CentOS

The command CD stands for change directory. CD can be used in many ways to navigate between multiple directories just like we traverse in GUI.

cd <filename>

The above command will move to the directory specified in the angular brackets. But you have to make sure that the name given here is present in the current directory. You can see what are the folders/files in the current directory using the command LS. Also you can check your current working directory using the command PWD.

cd /path/to/another/folder

The above command will take to another folder that is far away from the current working directory. Make sure that the specified path exists. You can check the path of the current structure using the command TREE.

cd ..

The above command will move the current working directory to one level back.

cd 

The above command without any arguments will take you to the home directory no matter where your current working directory is.

There is a video example for the same, watch it below

How to use “ls” command in Linux terminal

“ls” means list contents of a directory. We have various options with ls that can be used together to view files in a convenient way according to our requirement.

ls -a

ls with -a option shows all hidden files.

ls -l

ls with -l prints long listing format about each file. It consists of information like permissions(read/write/execute), owner, file size, file type(directory or file or symbolic link), last modified time and the name of the file.

ls -l can be used with another options like -t or/and -r. “-t” will sort files by modified time and “-r” reverses the order they are being sorted.

To view examples for above commands see the video below.

Setting Reverse Proxy in Apache Ubuntu

Suppose we have a web app running on a port. For example nodejs runs server on a port. For accepting incoming connections on this port without actually exposing the port number is what we look for.

To solve this we can add few lines in our apache configuration to tell our web server to accept connections on a port.

Lets assume we are trying to access this url

http://127.0.0.1:8080/mybankapp

Open the file /etc/apache2/sites-available/000-default.conf using any editor and paste the below lines. You may take a backup of the above file before doing so.

<VirtualHost *:80>
    ProxyPreserveHost On

    ProxyPass /mybankapp http://127.0.0.1:8080/mybankapp
    ProxyPassReverse /mybankapp http://127.0.0.1:8080/mybankapp
</VirtualHost>

Before we restart apache we may enable some modules.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

After we execute above command now we may restart apache.

sudo systemctl restart apache2

Now we can access our web app without the port number.

View Difference between Files and Folders Graphically in Linux With Meld

There are terminal utilities like ‘diff’ and ‘vimdiff’ in Linux to visualise the differences in two files. But sometimes this visual presentation can be difficult to be viewed and understanding it might take some time as it can be confusing to analyse.

Using meld we can visually the differences in a less confusing way and understand it more easily. Here is a screenshot of the same.

Bonus advantage is that we can view the graphical difference of two folders also.

To install Meld you can install from command line using the following command

sudo apt install meld

Swap two columns using Vim in Linux

Suppose you have a file seperated with a delimiter like tab or comma and you want to swap them manually it would take so much time if the file is huge. But if you want to do it using just a single command it would take 5 seconds.

Here is how it is done.

  • Open the file to be edited using following command.
  • vim file.txt
  • Then enter the following substitution commnad.
  • %s/\(.*\)\t(.*\)/\2\t\1/g
  • That’s it you have swapped two columns.
  • Now just save the file and quit vim using :wq

There is a video of the same below

How to use bc command for addition, subtraction, multiplication and division. Use Linux terminal as calculator

Linux command line is very powerful. Let us see how can we use it as a calculator.

Open a terminal window and write bc -ql.

Here there are two options. ‘q’ stands for quiet mode which means it doesnot print un necessary things. ‘l’ stands for mathlib which will operate mostly on decimals and other maths operations.

For addition.

For Subtraction

For Multiplication

For division

To quit the bc command type in ‘quit’.

To do more using bc command open man bc there you can see about more of its usage.