Browse Source

Adiciona merger de PDF

pull/3139/head
eribeiro 6 years ago
parent
commit
77585f7492
  1. 6
      sapl/materia/urls.py
  2. 55
      sapl/materia/views.py
  3. 3
      sapl/templates/materia/documentoacessorio_list.html

6
sapl/materia/urls.py

@ -27,7 +27,7 @@ from sapl.materia.views import (AcompanhamentoConfirmarView,
proposicao_texto, recuperar_materia,
ExcluirTramitacaoEmLoteView, RetornarProposicao,
MateriaPesquisaSimplesView,
DespachoInicialMultiCreateView, get_zip_docacessorios)
DespachoInicialMultiCreateView, get_zip_docacessorios, get_pdf_docacessorios)
from sapl.norma.views import NormaPesquisaSimplesView
from sapl.protocoloadm.views import (
FichaPesquisaAdmView, FichaSelecionaAdmView)
@ -119,7 +119,9 @@ urlpatterns_materia = [
url(r'^materia/excluir-tramitacao-em-lote', ExcluirTramitacaoEmLoteView.as_view(),
name='excluir_tramitacao_em_lote'),
url(r'^materia/docacessorio/zip/(?P<pk>\d+)$', get_zip_docacessorios,
name='compress_docacessorios')
name='compress_docacessorios'),
url(r'^materia/docacessorio/pdf/(?P<pk>\d+)$', get_pdf_docacessorios,
name='merge_docacessorios')
]

55
sapl/materia/views.py

@ -2727,22 +2727,6 @@ class TipoMateriaCrud(CrudAux):
return fv
def create_merged_docacessorio(mat_pk):
from PyPDF4 import PdfFileReader, PdfFileMerger
# reader1 = PdfFileReader("/tmp/sbm_imprimir_pedido__100047424.pdf")
output = "/tmp/merged.pdf"
merger = PdfFileMerger(open(output, "wb"))
input1 = open("/tmp/sbm_imprimir_pedido__100047424.pdf", "rb")
merger.append(fileobj=input1)
merger.append(fileobj=input1)
merger.append(fileobj=input1)
merger.write()
merger.close()
pass
def create_zip_docacessorios(mat_pk):
import os
import time
@ -2776,3 +2760,42 @@ def get_zip_docacessorios(request, pk):
% external_name)
return response
def create_pdf_docacessorios(mat_pk):
import os
import time
from datetime import datetime
from sapl.settings import MEDIA_ROOT
from PyPDF4 import PdfFileReader, PdfFileMerger
materia = MateriaLegislativa.objects.get(id=mat_pk)
docs = materia.documentoacessorio_set. \
all().values_list('arquivo', flat=True)
# TODO: o for-comprehension abaixo filtra os arquivos não PDF.
# TODO: o que fazer com os arquivos não PDF? converter? ignorar?
docs_path = [os.path.join(MEDIA_ROOT, i) for i in docs if i.lower().endswith('pdf')]
merged_pdf = '/tmp/mat_{}_{}.pdf'.format(
mat_pk,
time.mktime(datetime.now().timetuple()))
merger = PdfFileMerger()
for f in docs_path:
merger.append(fileobj=f)
merger.write(fileobj=open(merged_pdf, "wb"))
merger.close()
external_name = "{}_{}.pdf".format(materia.numero, materia.ano)
return external_name, merged_pdf
def get_pdf_docacessorios(request, pk):
import os
external_name, pdffilename = create_pdf_docacessorios(pk)
with open(os.path.join('/tmp', pdffilename), 'rb') as f:
data = f.read()
response = HttpResponse(data, content_type='application/pdf')
response['Content-Disposition'] = ('attachment; filename="%s"'
% external_name)
return response

3
sapl/templates/materia/documentoacessorio_list.html

@ -2,6 +2,9 @@
{% load i18n %}
{% block base_content %}
{{ block.super }}
<div class="actions btn-group float-right" role="group">
<a href="{% url 'sapl.materia:merge_docacessorios' root_pk %}" class="btn btn-outline-primary">{% trans 'Baixar documentos como PDF único' %}</a>
</div>
<div class="actions btn-group float-right" role="group">
<a href="{% url 'sapl.materia:compress_docacessorios' root_pk %}" class="btn btn-outline-primary">{% trans 'Baixar documentos compactados' %}</a>
</div>

Loading…
Cancel
Save