I’m not good at Django. I asked the django community to help me with a problem.
I want to create a form to upload a pdf file to the database
, then take that file out to add qr code at the top of the first page . Then save it in databse . I have sketched my idea. If Django can’t solve it, please give me a solution. I’m writing a functional website in django. Please help don’t reject me
What parts of this do you already know?
Do you know how to create a form?
Do you know how to handle uploaded files?
Do you know how to create a QR image?
Do you know how to add an image to an existing pdf page?
These are all effectively independent steps to this process. So break the larger problem into a set of smaller ones and address them one at a time.
i don’t know ! I need help with these 2 parts.
“I don’t know how to get the pdf file in the database and manipulate it in the “views.py” (insert the qr code in the first page). then save it back to the database.”
Ok, so these aren’t Django-related issues. Someone here may know the answers to this, but you might find more direct help in other forums. There’s nothing special about doing this in a view - if you can do it in a regular Python script, you can add that code to your view.
Once you have a program working to create the QR and insert it into your PDF, we can help you integrate that into your Django system.
Django can’t, so can Python?
Django community only you support ?
I’ve searched a lot but can’t find anyone with this problem - hope you have a specific direction to help me! thanks
Tutorial to generate QR code with Python:
To manipulate PDF in python you can use ReportLab
ReportLab Tutorial
https://www.reportlab.com/dev/docs/tutorial/
Intro to ReportLab with Django
There is also a other library: Borb. It seems nice, but I never tried it ! Pay attention to the licence of this one…
To be clear on something - Python is a programming language. Django is a program, written in the Python language.
So yes, as the links above show, this can be done in Python. However, you need to use programs and libraries other than Django to do it.
Since those functions are independent of Django, you would just be searching for information about QR Codes and PDF manipulations - Django is not part of that search.
Once you have code that will perform those functions, we can show you how to use that code with Django.
thanks !!! (^-^) .
Has anyone done the complete function of inserting qrcode into a pdf file to save the database?
from reportlab.graphics.barcode import code39, code128, code93
from reportlab.graphics.barcode import eanbc, qr, usps
from reportlab.graphics.shapes import Drawing
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
#----------------------------------------------------------------------
def olo():
packet = io.BytesIO()
# Create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.setFont('Helvetica-Bold', 14)
can.drawString(400, 20, "DONGSAPA.NET")
can.showPage()
can.save()
# Move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# Read your existing PDF
existing_pdf = PdfFileReader(open("original.pdf", "rb"))
output = PdfFileWriter()
# Add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# Finally, write "output" to a real file
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
def createBarCodes():
"""
Create barcode examples and embed in a PDF
"""
c = canvas.Canvas("original.pdf", pagesize=letter)
barcode_value = "1234567890"
barcode39 = code39.Extended39(barcode_value)
barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1)
# code93 also has an Extended and MultiWidth version
barcode93 = code93.Standard93(barcode_value)
barcode128 = code128.Code128(barcode_value)
# the multiwidth barcode appears to be broken
barcode128Multi = code128.MultiWidthBarcode(barcode_value)
barcode_usps = usps.POSTNET("50158-9999")
codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps]
x = 1 * mm
y = 285 * mm
x1 = 6.4 * mm
for code in codes:
code.drawOn(c, x, y)
y = y - 15 * mm
# draw the eanbc8 code
barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value)
bounds = barcode_eanbc8.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(50, 10)
d.add(barcode_eanbc8)
renderPDF.draw(d, c, 15, 555)
# draw the eanbc13 code
barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value)
bounds = barcode_eanbc13.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(50, 10)
d.add(barcode_eanbc13)
renderPDF.draw(d, c, 15, 465)
# draw a QR code
qr_code = qr.QrCodeWidget('dongsapa.net')
bounds = qr_code.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = Drawing(45, 45, transform=[45./width,0,0,45./height,0,0])
d.add(qr_code)
renderPDF.draw(d, c, 15, 405)
c.save()
if name == “main”:
createBarCodes()
olo()
I want to open the pdf file in the databse to process and then save it!!! help me
Do you want to save the file-data itself as a column in the database, or as a file with a reference to it?
If you want the actual file stored as data in the database, see BinaryField - paying particular note to the “Abusing BinaryField” note in that section.
If you want to store the file as a file in the file system, while maintaining a reference to that file in the database, see:
- The File object | Django documentation | Django
- File storage API | Django documentation | Django
- Uploaded Files and Upload Handlers | Django documentation | Django
If you search through this board, you’ll also find a number of different conversations about handling uploaded files.
i want to save as file to databse . Can you give me an example code?
You’re not saving a “file” to the database. You’re saving the data from the file in a BinaryField.
So for example, if a file is uploaded to a form field named uploaded_file
, you can populate the BinaryField using uploaded_file.read()
.