In this article, I'll introduce
Python to the Ubuntu 24.04 environment on AWS Lightsail and introduce the steps to be able to generate PDFs containing Japanese. The
development directory was named “testpy”.
1. Preparing a working directory
.First, create a working folder in your home directory.
mkdir -p ~/testpy && cd ~/testpy I'm going to put Python's virtual environment and scripts here.
The work is performed by an
ubuntu user. 🧠 2. Python and virtual environment setup
.Ubuntu 24.04 includes Python 3.12 as standard, but just in case, update before creating a virtual environment.
sudo apt update sudo apt install -y Python3 Python3-pip Python3-venv Create virtual environment: Python3 -m venv.
venv activation (
activation): If (.venv) appears at the top of the
source.venv/bin/activate prompt, it is successful. Python and pip running in this state will be dedicated to virtual environments.
🧩 3. Operation confirmed.
Check if Python works with a simple script.
echo 'print (“Hello, TechFan! Python is running.”) ' > test.py python test.py “Hello, TechFan!” If it is displayed, it
's OK.🖋️ 4. PDF related libraries were introduced
.Add the libraries needed to generate and edit PDFs.
pip install reportlab pypdf pillow 🧾 5. English PDF generation test
.python - 'PY'
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
c = canvas.canvas (” test_reportlab.pdf “, pageSize=A4)
w, h = A4
c.setFont (“Helvetica-Bold”, 20)
c.drawCentredString (w/2, h-40*mm, “TechFan PDF Test”)
C.setFont (“Helvetica”, 12)
c.drawstring (20*mm, h-60*mm, “ReportLab generated this page.”)
C.showpage ()
c.save ()
print (“OK: test_reportlab.pdf “)
PY If the file test_reportlab.pdf is generated, it is successful.
6. Introduced Japanese fonts
.If the Japanese characters are garbled, add a Japanese font.
sudo apt install -y fonts-ipaexfont fontconfig fc-list | grep -i ipaex output example: /usr/share/fonts/
opentype/ipaexfont-gothic/ipaexg.ttf: iPAEXgothic, iPAEX gothic: style=regular 7. Japanese PDF generation test
.python - 'PY'
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
FONT_PATH =” /usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf”
PDFMetrics.registerFont (ttFont (“iPAEXGothic”, FONT_PATH))
c = canvas.canvas (” test_japanese.pdf “, pageSize=A4)
c.setFont (“iPAEXGothic”, 18)
C.drawstring (72, 780, “Japanese Test: TechFan Pass Certificate Sample”)
C.drawstring (72, 752, “name: Yamada Taro”)
c.drawstring (72, 724, “Exam name: Python Basic Test”)
c.drawstring (72, 696, “Congratulations!”)
C.showpage ()
c.save ()
print (“OK: test_japanese.pdf “)
PY It's OK if Japanese is displayed correctly in the output PDF.
🔁 8. Switch the virtual environment (
on/off).Activate (start using):
source ~/testpy/.venv/bin/activateDisable (end):
deactivate
This mechanism is an image of opening and closing the Python environment on a work basis, similar to “fopen/fclose
”.✅ Summary.
Now, the preparation for the Python virtual environment, the
After that, we can build a pass certificate issuance system etc. based on this environment.