First Task [Report Work in Pdf]

Today I am working on Reportlab. This library used for pdf generate . I  learn how to create PDF using Reportlab.

there is some code how to use this library

1) create your project startproject pdf

2) Create your Application

cd pdf startapp hellos

3) Do this simple coding for model

from django.db import models
from django.utils.encoding import smart_unicode

class Hello(models.Model):
first_name = models.CharField(max_length=200)

def __unicode__(self):
return smart_unicode(self.first_name)

4) Now this code for admin File

from django.contrib import admin
from .models import Hello
from reportlab.pdfgen import canvas
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch

class HelloAdmin(admin.ModelAdmin):
class Meta:
model = Hello

//this code for pdf generation
canvas = canvas.Canvas("form.pdf", pagesize=letter)
canvas.drawString(30,760, 'GURU NANAK DEV ENGINEERING COLLEGE')
canvas.drawString(30,730, 'No. GNDEC/TCC/R/____')
canvas.drawString(500,730, 'Dated: . . .')
canvas.drawString(30,700, 'To')
canvas.drawString(50,680, 'Hello')
canvas.drawString(50,660, 'Ludhiana')
canvas.drawString(30, 640, 'Sub:')
canvas.drawString(100,640, 'Test report of Concrete Cubes [nm]')
canvas.drawString(30,620, 'Ref:')
canvas.drawString(100,620, 'Your letter dated nn.')
canvas.drawString(30,600, 'sir,')
canvas.drawString(100,580, 'The result of concrete cubes sent by you tested on are as under:')
canvas.drawString(30,200, 'Conditions:')
canvas.drawString(100,180, '1. This test refers only to samples submitted for the test')
canvas.drawString(100,160, '2. This test shall not used for any sales promotions or advertisements without permission.')
canvas.drawString(100,140, '3. This test report is not to be used for legal purpose & will not be produced in the Court of Law.')
canvas.drawString(300,60,'Dean Testing & Consultancy'),HelloAdmin)

5) Now run you application

python makemigrations

python migrate

python runserver

6)Open your browser run application.

7)After run your project you found your PDF in your application folder.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s