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

django-admin.py startproject pdf

2) Create your Application

cd pdf

django-admin.py 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.setLineWidth(.3)
canvas.setFont('Helvetica',12)
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 No.mn 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(30,60,'(Co-ordinator)')
canvas.drawString(300,60,'Dean Testing & Consultancy')
canvas.save()

admin.site.register(Hello,HelloAdmin)

5) Now run you application

python manage.py makemigrations

python manage.py migrate

python manage.py runserver

6)Open your browser run application.

127.0.0.1:8000/

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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