What is an SVG File?
SVG stands for Scalable Vector Graphics format, and it is used for saving graphic files. The vector files are saved according to a mathematical formula based on the points and lines of a grid.
What is a PDF File?
PDF stands for Portable Document Format, and it is considered one of the most commonly used formats for creating and transferring documents from one place to another. The biggest advantage of this format is that it preserves the document’s original formatting, making it convenient for you to take its printout later without needing to fix the formatting issues.
Pre-Requisite of Performing the SVG and PDF Conversion to Base64
Since we will be performing the conversion of the SVG and PDF to Base64 by using the Python programming language, it is mandatory to have Python installed on our Ubuntu 20.04 system. For this particular guide, we have used Python3.
General Process of SVG to Base64 Conversion
If we specifically talk about the Python programming language, then, for converting an SVG file to Base64 in this language, we first need to locate and open the SVG file that needs to be converted to Base64. After opening this file, it is read, followed by the encoding process in Python.
Method of Converting an SVG File to Base64 using Python in Ubuntu 20.04
For converting an SVG file to Base64 using the Python programming language in Ubuntu 20.04, you will have to use the Python script shown in the following image:
We first created an empty file in our home directory with the “.py” extension. We have named this file “SVGtoBase64”. You can also have any other name of your preference for this file. Then, we opened this file to write our Python script within it. After that, we imported the “base64” library into our script so that we could conveniently convert our SVG file to Base64. Then, we used the “with open” function to read our SVG file and passed the name of our target SVG file, which was “image.svg”.
You can pass any SVG file of your choice that you want to be converted to the Base64 format. Then, we have also specified the mode for opening this file which is “rb” in this case. The “rb” mode opens a file for reading in binary format. Moreover, we have also specified that we need to open this file as an image file.
Then, we have created a variable named “encoded_string” to hold our output. We have equalized our encoded string to the function “base64.b64encode” and passed the image file we opened for reading. Finally, to show the user that converting the SVG file to Base64 has been carried out successfully, we have printed the value of the “encoded_string” variable on the terminal by using the “print” command of the Python programming language.
Now, for performing the actual conversion of the SVG file to Base64, we need to execute the Python script that we have created above by using the command shown below:
You can verify from the following output that the specified SVG file has been successfully converted to Base64 using the Python programming language:
General Process of PDF to Base64 Conversion
As far as the general process of converting a PDF file to Base64 in the Python programming language is concerned, we first need to open that specific PDF file for reading. After reading this file, it is encoded using Python’s built-in encoding function.
Method of Converting a PDF File to Base64 using Python in Ubuntu 20.04
We can easily convert a PDF file to Base64 using the Python programming language in Ubuntu 20.04. For doing so, we have designed the Python script shown in the image below:
Again, in this Python script, we have imported the “base64” library, without which it would not have been possible to convert a PDF file to Base64 in Python. Then, we have opened our target PDF file with the help of the “with open” function and have passed to it the name of our PDF file, i.e., sample.pdf. We have specified the mode “rb” to open this file for reading in the binary format.
Also, we have specified that we want to open this file as a PDF. Then, we have created a variable named “encoded_string” and have equalized it to the function “base64.b64encode”. We have passed the PDF file that we have opened above to this function. Finally, we have used the Python programming language’s ” print ” command to print the encoded string on the terminal. After seeing this Python script, you can realize that the script discussed above is pretty much similar to this one.
For converting the target PDF file to Base64 using the Python programming language in Ubuntu 20.04, we have executed the following command:
The result shown in the image below confirms that the specified PDF file has been successfully converted to Base64.
This article was meant to explain to you the process of converting the SVG and PDF files to Base64. We first talked briefly about these file formats, followed by their respective conversion procedures in Base64 using the Python programming language in Ubuntu 20.04. By following these scripts, you can easily convert any of your desired SVG and PDF files to Base64.