Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
875906c2
Verified
Commit
875906c2
authored
4 years ago
by
Jonathan Weth
Browse files
Options
Downloads
Patches
Plain Diff
Add tools for rendering PDFs with electron-pdf
parent
186c111c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!464
Resolve "Support PDF generation via a headless Chromium running in the background using celery"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
aleksis/core/util/pdf.py
+90
-0
90 additions, 0 deletions
aleksis/core/util/pdf.py
docs/dev/01_setup.rst
+2
-0
2 additions, 0 deletions
docs/dev/01_setup.rst
with
92 additions
and
0 deletions
aleksis/core/util/pdf.py
0 → 100644
+
90
−
0
View file @
875906c2
import
glob
import
os
import
subprocess
# noqa
from
tempfile
import
mkstemp
from
typing
import
Union
from
django.http.request
import
HttpRequest
from
django.http.response
import
HttpResponse
from
django.shortcuts
import
render
from
django.template.loader
import
render_to_string
from
django.utils.translation
import
gettext
as
_
from
celery_progress.backend
import
ProgressRecorder
from
aleksis.core.settings
import
MEDIA_ROOT
,
MEDIA_URL
,
STATIC_ROOT
from
aleksis.core.util.core_helpers
import
(
DummyRecorder
,
celery_optional
,
celery_optional_progress
,
is_celery_enabled
,
path_and_rename
,
)
@celery_optional_progress
def
generate_pdf
(
recorder
:
Union
[
ProgressRecorder
,
DummyRecorder
],
html_code
:
str
,
pdf_path
:
str
):
"""
Generate a PDF file by rendering the HTML code using electron-pdf.
"""
recorder
.
total
=
1
# Replace /static with STATIC_ROOT to get local file system paths
html_code
=
html_code
.
replace
(
"
/static
"
,
STATIC_ROOT
)
# Write HTML code to a temporary file to make it available for electron-pdf
f
,
path
=
mkstemp
(
"
.html
"
)
with
open
(
path
,
"
w
"
)
as
f
:
f
.
write
(
html_code
)
subprocess
.
run
([
"
electron-pdf
"
,
path
,
pdf_path
])
# noqa
recorder
.
set_progress
(
1
)
def
render_pdf
(
request
:
HttpRequest
,
template_name
:
str
,
context
:
dict
=
None
)
->
HttpResponse
:
"""
Start PDF generation and show progress page.
The progress page will redirect to the PDF after completion.
.. warning::
If celery isn
'
t enabled, this will render the template directly in the browser.
"""
if
not
context
:
context
=
{}
pdf_path
=
path_and_rename
(
None
,
"
file.pdf
"
,
"
pdfs
"
)
html_template
=
render_to_string
(
template_name
,
context
)
if
is_celery_enabled
():
result
=
generate_pdf
(
html_template
,
os
.
path
.
join
(
MEDIA_ROOT
,
pdf_path
))
context
=
{
"
title
"
:
_
(
"
Progress: Generate PDF file
"
),
"
back_url
"
:
context
.
get
(
"
back_url
"
,
"
index
"
),
"
progress
"
:
{
"
task_id
"
:
result
.
task_id
,
"
title
"
:
_
(
"
Generating PDF file …
"
),
"
success
"
:
_
(
"
The PDF file has been generated successfully.
"
),
"
error
"
:
_
(
"
There was a problem while generating the PDF file.
"
),
"
redirect_on_success
"
:
MEDIA_URL
+
pdf_path
,
},
"
additional_button
"
:
{
"
href
"
:
MEDIA_URL
+
pdf_path
,
"
caption
"
:
_
(
"
Download PDF
"
),
"
icon
"
:
"
picture_as_pdf
"
,
},
}
# Render progress view
return
render
(
request
,
"
core/pages/progress.html
"
,
context
)
else
:
# Render PaperCSS view if Celery isn't enabled
return
render
(
request
,
template_name
,
context
)
@celery_optional
def
clean_up_pdf_directory
()
->
None
:
"""
Clean up directory with generated PDF files.
"""
files
=
glob
.
glob
(
os
.
path
.
join
(
MEDIA_ROOT
,
"
pdfs
"
,
"
*.pdf
"
))
for
file
in
files
:
os
.
remove
(
file
)
This diff is collapsed.
Click to expand it.
docs/dev/01_setup.rst
+
2
−
0
View file @
875906c2
...
...
@@ -30,6 +30,8 @@ Some system libraries are required to install AlekSIS::
sudo apt install build-essential libpq-dev libpq5 libssl-dev python3-dev python3-pip python3-venv yarnpkg gettext
If you want to use the PDF rendering feature, you have to install ``electron-pdf``
according to the instructions at https://github.com/fraserxu/electron-pdf.
Get Poetry
----------
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment