Skip to content

Instantly share code, notes, and snippets.

@osw4l
Created June 4, 2018 15:33
Show Gist options
  • Save osw4l/474826ba596902b7d417e79a2cd2d9cf to your computer and use it in GitHub Desktop.
Save osw4l/474826ba596902b7d417e79a2cd2d9cf to your computer and use it in GitHub Desktop.
@shared_task
def generate_pdf(user_id, body):
user = User.objects.get(id=user_id)
files = body['new_files']
tabs = {}
for pic in files:
file = FileDetail.objects.get(filename=pic['filename'])
tab = file.story_set.get().channel.tab_set.first()
logger.info('channel {}'.format(file.story_set.get().channel))
file.channel = file.story_set.get().channel.name
if tab.name not in tabs.keys():
tabs[tab.name] = []
tabs[tab.name].append(file)
html_string = render_to_string('pdf.html', {
'tabs': tabs,
'title': body['title']
})
html = HTML(string=html_string)
html.write_pdf(target='/tmp/file.pdf')
fs = FileSystemStorage('/tmp')
with fs.open('file.pdf') as pdf:
path = default_storage.save('tmp/' + str(pdf), ContentFile(pdf.read()))
tmp_file_path = os.path.join(settings.MEDIA_ROOT, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment