|
|
@@ -111,4 +111,29 @@ def generate_pdf(request, pk):
|
|
|
pdf = html.write_pdf()
|
|
|
response = HttpResponse(pdf, content_type='application/pdf')
|
|
|
response['Content-Disposition'] = f'attachment; filename="{document.title}.pdf"'
|
|
|
+ return response
|
|
|
+
|
|
|
+@login_required
|
|
|
+def generate_version_pdf(request, pk, version_number):
|
|
|
+ """Generates a PDF for a SPECIFIC historical version of a document."""
|
|
|
+ # First, get the parent document to ensure ownership
|
|
|
+ document = get_object_or_404(Document, pk=pk, author=request.user)
|
|
|
+
|
|
|
+ # Now, get the specific version associated with that document
|
|
|
+ version = get_object_or_404(DocumentVersion, document=document, version_number=version_number)
|
|
|
+
|
|
|
+ # We'll create a dedicated PDF template for versions to keep things clean
|
|
|
+ html_string = render_to_string('textdocs/version_pdf.html', {'version': version})
|
|
|
+
|
|
|
+ # Generate the PDF with WeasyPrint
|
|
|
+ html = HTML(string=html_string, base_url=request.build_absolute_uri())
|
|
|
+ pdf = html.write_pdf()
|
|
|
+
|
|
|
+ # Create the HTTP response with the PDF file
|
|
|
+ response = HttpResponse(pdf, content_type='application/pdf')
|
|
|
+
|
|
|
+ # Make the filename include the version number for clarity
|
|
|
+ filename = f'{version.document.title}_v{version.version_number}.pdf'
|
|
|
+ response['Content-Disposition'] = f'attachment; filename="{filename}"'
|
|
|
+
|
|
|
return response
|