alexmorozov / templated-docs Goto Github PK
View Code? Open in Web Editor NEWGenerate PDF, MS Word and Excel documents from templates in Django
License: MIT License
Generate PDF, MS Word and Excel documents from templates in Django
License: MIT License
Hi, I am trying to test this package on local mac machine. I found that it needs pylokit
from pylokit import Office
But it seems pylokit only support linux.
[https://github.com/xrmx/pylokit/blob/master/pylokit/lokit.py]
I am trying add:
`/Applications/OpenOffice.app/Contents/program` in TEMPLATED_DOCS_LIBREOFFICE_PATH,
but there are no `TARGET_LIB = ("libsofficeapp.so", "libmergedlo.so")`,
in mac, there is only `libsofficeapp.dylib`.
I do not know how to set TEMPLATED_DOCS_LIBREOFFICE_PATH for environment var.
Any suggestions?
Error message:
raise LoKitInitializeError("Failed to initialize LibreOfficeKit") pylokit.lokit.LoKitInitializeError: Failed to initialize LibreOfficeKit
I have installed LibreOffice 5, and added some data in database
`python manage.py runserver `
after I input information, I clicked the button, then I got that error.
Why this happen? How to solve this ?
I can successfully generate .docx files in development (Ubuntu 16.04), but it hangs/times out on when deployed to the server (Ubuntu 14.04).
I have tried LO versions 5.1.6.2 and 5.3.2.2.
Nginx & Gunicorn timeouts are 120s. It will successfully generate the templates in 2-3 seconds on my laptop, so the timeout is not the issue. The processor is idle throughout.
Edit:
It seems I am able to create .odt files, but not .doc. or .docx on the server.
I've added logging to every step of process and discovered that it doesn't seem to get past
https://github.com/alexmorozov/templated-docs/blob/master/templated_docs/__init__.py#L92
This would indicate a problem with LO.
On the server, I can successfully convert an .odt file to .docx with the pylokit example...
>>> import os
>>> lo_path = "/usr/lib/libreoffice/program"
>>> with Office(lo_path) as lo:
... with lo.documentLoad("report_end_of_course.odt") as doc:
... doc.saveAs("myNewFile.docx")
Is there anything I can try to debug this further?
Thanks
When coding Django's tags via LibreOffice window, we are practically unable to control the way LibreOffice stores them in its messy zipped od*
archive. One may happen that Django's tags are messed up with xml
tags as shown below:
<text:p text:style-name="P21">Lorem ipsum {{
<text:span text:style-name="T12">element.el0 }</text:span>}
<text:span text:style-name="T12">and {{ element.el1 }} and {{ element.el2 }} and </text:span>{{
<text:span text:style-name="T12">element.eln }</text:span>}
</text:p>
So templated-docs
unzips template and Django replaces its tags with data. But how it works with markup like this }</text:span>}
? Obviously, source xml
will be broken and, furthermore, od*
corrupted.
Maybe here is the explanation of the issue #8
I don't know if there's any way to fix the issue except programming source xml
.
I have passed a dictionary of context which I had to display in the excel.
Most of them were numbers but in the excel created as the output of templated_docs, these numbers were seen as text and I was not able to do math operations such as "SUM"
Expected result: The type of variable in the Excel as "Integer"
No crash. The data was coming as it is exact. But only the "type" is different.
I tried making a template with default Format as "Number" still the same output is repeated
From docs
Django templating language is fully supported, including filters and tags.
Is that really true? At least safe
filter does not seem to work?
We should be able to make a certain part of a document a hyperlink. At the first glance, a custom template tag will suffice:
{% hyperlink 'http://site.com' %}
here goes a part of a document
{% endhyperlink %}
When i generate odt from odt template that is filled with data i get
corrupted file libreoffice says:
Read-Error.
Format error discovered in the file in sub-document content.xml at 2,199655(row,col).
locate libmergedlo.so
is nothingTraceback (most recent call last):
File "/usr/lib/python3.4/multiprocessing/process.py", line 254, in _bootstrap
self.run()
File "/usr/lib/python3.4/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.4/dist-packages/templated_docs/__init__.py", line 92, in _convert_subprocess
with Office(lo_path) as lo:
File "/usr/local/lib/python3.4/dist-packages/pylokit/lokit.py", line 110, in __init__
self.lokit = lo.libreofficekit_hook(six.b(lo_path))
File "/usr/local/lib/python3.4/dist-packages/cffi/api.py", line 866, in __getattr__
make_accessor(name)
File "/usr/local/lib/python3.4/dist-packages/cffi/api.py", line 862, in make_accessor
accessors[name](name)
File "/usr/local/lib/python3.4/dist-packages/cffi/api.py", line 792, in accessor_function
value = backendlib.load_function(BType, name)
AttributeError: function/symbol 'libreofficekit_hook' not found in library '/usr/lib/libreoffice/program/libmergedlo.so': /usr/lib/libreoffice/program/libmergedlo.so: undefined symbol: libreofficekit_hook
Im trying to pass fill_template('dashboard/report/word/invoice.odt', context, output_format='pdf')
def report_html_view(request,project_id):
try :
context = {'number':'93333333'} # Just an example
filename = fill_template('dashboard/report/word/invoice.odt', context, output_format='pdf')
visible_filename = 'greeting.pdf'
except Exception as e:
print('Something Went Wrong.!'+ str(e))
return FileResponse(filename, visible_filename)
get_template_sources() takes 2 positional arguments but 3 were given
when trying to use a template of .fodt
format, it throws error that it is not a valid zip file. Upon inspecting code, the fill_template
method is written to take only zip file into consideration. I wrote code to consider both kind flat/archive formatted documents. I will create a PR with that.
When I generate the pdf with images, in the generated PDF only a white image with a border instead of the original image appears
VIEWS.PY
context = {}
attach = Attachment()
soup = BeautifulSoup(description)
descriptions = soup.findAll('p')
for d in descriptions:
token = str(d)[3:-4]
if token[:9] == '<img src=':
format, imgstr = token.split(';base64,')
ext = format.split('/')[-1]
data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext)
attach.attach = data
attach.save()
dd.append(attach.attach)
context['dd'] = dd
filename = fill_template('invoice/invoice2.odt', context, output_format='pdf')
visible_filename = 'invoice.{}'.format('pdf')
return FileResponse(filename, visible_filename)
INVOICE2.ODT
{% load templated_docs_tags %}
{% for d in dd %}
{% image d %}
{% endfor %}
Document a way to save files in a FileField.
I faced a critical bug which kills Django server with exit codes 139, 77.
I have an API which handles .docx render. I will show one of views, as an example, they are pretty similar:
@permission_classes((permissions.IsAdminUser,))
class BookDocxViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
def retrieve(self, request, *args, **kwargs):
queryset = Pupils.objects.get(id=kwargs['pk'])
serializer = StudentSerializer(queryset)
context = dict(serializer.data)
doc = fill_template('crm/docs/book.ott', context, output_format='docx')
p = u'docs/books/%s/%s_%s_%s.doc' % (datetime.now().date(), context[u'surname'], context[u'name'], datetime.now().date())
with open(doc, 'rb') as f:
content = f.read()
path = default_storage.save(p, ContentFile(content))
f.close()
return response.Response(u'/media/' + path)
When I call it the first time, it creates a .docx file, saves it to my default_storage and then returns me a download link. But when I try to do it again, or do it with another method (which works with another template and context), my server just crashes without any logs. The last thing I see is either
Process finished with exit code 77 if I call it with a little delay (more then one second)
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) if call my method for the second time right away (in less than one second)
I tried to use debuger -- it said that my server crashes on this line:
doc = fill_template('crm/docs/book.ott', context, output_format='docx')
I bet what happens is:
According to this documentation, the process that used pylockt
should be killed after. But we cannot kill Django server.
I made some changes to __init__.py
to add multiprocessing. Pull request is attached.
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
django 2.0.2
does not generate files ... ;-/ even demo project does not work
Not really a Issue, but more a feature: Allow absolute path for template odt, as I would like to not use a template from template folder.
One might be able to get a file from a Model FileField and use it as the odt model. This may be accomplished by trying to get the template from the provided path first, if it fails, try loading from the template folder (or vice versa).
I'll try to implement this on my fork and pull request it later.
Thanks for this awesome project, by the way. Also saw a nice jabber related project of yours. I am also interested on that one as I have some solution that sends. I'll test it later against the latest Spark Server.
I'm writing an application where users can provide there own templates, via uploading them.
from templated_docs import fill_template
from templated_docs.http import FileResponse
template_path = "/home/.../template.odt"
context = {... my pretty context ...}
filename = fill_template(template_path, context, "pdf")
pretty_name = "my_pretty_name.pdf"
return FileResponse(filename, pretty_name)
which raises a "TemplateDoesNotExist" exception.
I could use an external app to load the template (django-dbtemplate for instance), but loading the template right from the full path should be simpler.
Thanks !
I'm trying to use a base case example filling a receipt with data from my database in a Django App.
Everything seems to be working as expected, no errors in the Django development server nor requests, but I only get the file the first time that I make the GET request to the method that I'm using to generate de DOCX file with my data. Then if I call this method for second time and so on, the GET remains waiting for a response from server that nevers is produced.
It's sounds to me like if the first request is not ending correctly de libreoffice proccess being used to generate the template output, but I'm just guessing.
DEBUG SERVER SAMPLE OUTPUT:
Django version 1.9.10, using settings 'extrematic.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
create libreoffice object
Uno initialized 1
initialized
[29/Sep/2016 14:13:36] "GET /expediente/398/solicitud_pago/ HTTP/1.1" 200 62645
create libreoffice object
METHOD THAT I'M USING TO PASS DATA TO TEMPLATE:
@csrf_exempt
@staff_member_required
@require_http_methods(["GET",])
def solicitud_pago(request, expediente):
expediente = Expediente.objects.get(pk=expediente)
servicios = Servicio.objects.filter(expediente=expediente)
context = {'user': request.user, 'expediente': expediente, 'servicios': servicios, 'fecha': datetime.date.today() }
filename = fill_template('solicitud_pago.odt', context, output_format='docx')
visible_filename = 'solicitud_pago.docx'
return FileResponse(filename, visible_filename)
Thanks in advance for your help and for all the effort building this app.
Regards,
Javier Civantos
I'm getting DjangoUnicodeEncodeError for no apparrent reason.
Here's the error page code: http://dpaste.com/3QBF0F7
I'm trying to get some data from my database to a .pdf file for printing. I can't get this module to produce a pdf file at all. The django debug error site gives a hint for a .png and I can't locate any png files in my data request.
The only place I can find a mention for a png file is in the __init__.py
file line 99.
data = smart_str(source.read(name))
where a variable of name
has a value of Thumbnails/thumbnail.png
when the site is trying to produce the aforementioned file with the data I need.
The setup.py explicitly lists Django from 1.8 to < 1.11
Is there anything specific this doesn't work with in 1.11, or is this simply a "we've never tested with 1.11" defensive value?
While trying to expand an ImageField inside a template, we don't get the image in the resulting OpenOffice file.
we create a Django ImageField object, and insert into the context, then we expand the template (an ODS) to another ODS. Everything is expanded (variables and objects) but not the image (sort of)
we unzip the resulting ODS, we see the image tag with the link to the subdir "Pictures/<ourfile.jpg>" but there is no "Pictures/" subdir in the resulting zip file, so this to us explains why it doesn't work as expected.
We have looked at the source code of the image tag, and indeed there doesn't seem to be an instruction to write anything to the Pictures subdir.
There is something in the init.py file, that seems to write in the Pictures/ subdir but it doesn't work for us.
We are pretty ignorant of the ODT/S format, but to us it seems it could not work if there is no embedded file in the zip. Is this assumption correct? If so, could we propose a patch?
Thanks
Z
Hello, thank you very much for your contribution, hes a great help.
how I can load a template from a model?
Example: model templates with a filefield, I need to take this file as a template
Thank you for your cooperation
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.