loftylabs / django-hardcopy Goto Github PK
View Code? Open in Web Editor NEWRender PDFs from HTML in Python/Django using Headless Chrome
Home Page: http://hirelofty.com
Render PDFs from HTML in Python/Django using Headless Chrome
Home Page: http://hirelofty.com
Using django-hardcopy, with windows 10, and python 3.6 I have found that it just hangs on the subprocess.call(). I think the reason for this is that, when python creates a NamedTemporaryFile, in NT it is created with the FILE_SHARE_DELETE flag, so that windows (not python) automatically deletes the file after it is closed. It also allows, the file to be opened a second time, when already open but it has to be opened with the FILE_SHARE_DELETE flag. As a result chrome can’t write to it or overwrite it. Fortunately, Django have created there own NamedTemporaryFile, (see here and here)
Replacing this line:
from tempfile import NamedTemporaryFile
with this:
from django.core.files.temp import NamedTemporaryFile
fixes everything for me. I have not tested this on any other operating systems, but it doesn’t look like it would cause any issues.
Would it be at all possible to record a video from headless chrome instead of capturing png or pdf? I know it is possible to do with selenium, but that's not an elegant solution..
What is the license for this fantastic library?
Can you kindly add a LICENSE file to your library. It will be useful to add the library for a project.
Hi there...I'm using django-hardcopy in Mac OSX natively with no issues. However, with the same Django app codebase I can't get it working in my Docker container. My Dockerfile is pretty basic:
FROM python:2.7.13-onbuild
RUN apt-get -y update && apt-get install -y --no-install-recommends less libboost-all-dev chromium
All I see in the Docker logs is this warning: [1227/175519.609298:WARNING:audio_manager.cc(321)] Multiple instances of AudioManager detected
which tells me chromium is at least doing something. But it fails to produce a PDF and all I get when browsing to the view is a Chrome PDF error "Failed to load PDF document." I forked your repo and enabled chromium logging but nothing additional shows up in the logs.
Based on this I made the assumption this library has been used in a Docker container in some form or fashion. Is my Docker container perhaps missing some necessary packages?
Requested by /u/kennydude on Reddit
Implement a Renderer class which renders PDF and returns them as a FileResponse when ?format=pdf
.
Unlike other renderer classes, this will require setting a template that receives the Serializers output as context.
please do add piplib
--print-to-pdf
[4] gets the job done but it appears that in the future this will be deprecated [0] [1] , based on my reading of the linked discussion threads.[0] https://superuser.com/questions/1281309/how-can-i-print-a-webpage-in-landscape-mode-using-headless-chromium-on-the-comma/1281535#1281535
[1] It is likely that this will lead to deprecation (and eventual removal) of --headless mode command line flags such as --print-to-pdf or --screenshot.
[2] https://github.com/fate0/pychrome
[3] https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
[4] https://github.com/loftylabs/django-hardcopy/blob/master/hardcopy/__init__.py#L31-L34
The arg --no-sandbox is hardcoded in the get_chrome_args() and can't be overwritten easily. This setting is a huge security flaw. Please make sure that we can override this and remove it from the default settings.
def get_chrome_args(): return [ settings.CHROME_PATH, '--no-sandbox', # Avoids permission issues while dockerized. '--headless', '--disable-extensions', # Reduces startup overhead. '--disable-gpu', # Required by chrome's headless mode for now. ]
Is there a way to remove headers and footers on pages in rendered pdf?
I need margins, but they must be blank.
I tried to make it with css:
@page{
margin: 0;
size: auto;
}
but so pages are without margin, that I need.
Is there a some key to call chrome?
tried to combine this solution with wrapping page content into div with styles:
.wrapper{
margin: 15mm 10mm 15mm 15mm;
}
but this helps only for first and last pages. pages between looks like they have no top and bottom margins.
I'd like to take a more active role in contribution and wanted to know if we could setup some sort of branching strategy. (Master < Release < Develop < Feature
) ... Where Master
would represent the active version on PyPI.
Also, having goals in mind and move towards ... Some Ideas:
Perhaps would could document this in a CONTRIB.md?
According to this blog post and other sources out there, chromium-browser is no longer usable from the apt-get install
method. They're forcing everyone to use snap instead. Trying to run chromium-browser form the apti-get install
results in this message in the terminal:
chromium-browser --help
Command '/usr/bin/chromium-browser' requires the chromium snap to be installed.
Please install it with:
snap install chromium
https://ubuntu.com/blog/chromium-in-ubuntu-deb-to-snap-transition
Independently of CHROME_PATH being set conf.get_chrome_path is always called.
If the system has none of the default paths exists this raises a ValueError.
conf.get_chrome path should only be called if CHROME_PATH is not defined.
With more and more PRs coming in for django-hardcopy
, I need to start adding test coverage and would welcome any help.
When hardcopy is used inside some daemon (i.e. rqworker) and called multiple times, it produces a lot of defunct chromium processes.:
687164 ? SLs 0:03 \_ /usr/local/bin/python -Wd ./manage.py rqworker higher
714982 ? Z 0:00 \_ [chromium] <defunct> 714983 ? Z 0:00 \_ [chromium] <defunct> 715003 ? ZN 0:00 \_ [chromium] <defunct> 718025 ? Z 0:00 \_ [chromium] <defunct> 718026 ? Z 0:00 \_ [chromium] <defunct> 718047 ? ZN 0:00 \_ [chromium] <defunct> 718964 ? Z 0:00 \_ [chromium] <defunct> 718965 ? Z 0:00 \_ [chromium] <defunct> 718984 ? ZN 0:00 \_ [chromium] <defunct>
Is there any way to clean up defunct processes after conversion?
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.