I am trying to create a PDF analysis web app and I am stuck. I want to allow the user to open a certain page of the pdf that have over 300 pages in it.
So, can anyone tell me how to use Django to open the pdf in a new tab on a specific page?
EDIT -- Actually the Django code is running on AWS server and I want the user to see and open a PDF on a specific page that is stored into my media folder after analysis.
The answer to this really isn't Django-specific and is going to depend on what viewer you are using to display the PDF. If you're relying on the browser to display it, according to Adobe, you can use an anchor link (#page=) in the url (e.g. http://www.example.com/myfile.pdf#page=XX) though I think support for this varies outside Firefox and Chrome.
If you're using PDF.JS, on the other hand, you'll be able to programatically select a page to render. You can see how to do that on the viewer's examples page, here.
This depends on which library etc.
Not really a django question as #ps_ said.
From Adobe.com
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link's URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
<A HREF="http://www.example.com/myfile.pdf#page=4">
I was able to solve my question, it took a while and after every attempt to use different libraries and PDF.js failed.
The solution is quite simple. so what I did is:
Step 1: Add MEDIA_ROOT and MEDIA_URL to settings.py.
# path to the MEDIA directory
MEDIA_ROOT = '/home/absolute/path/to/your/media/folder'
# URL to use to open MEDIA
MEDIA_URL = '/media/'
Step 2: Update the urls.py with the following code:
from . import settings
import django
urlpatterns = [
...
...
url(r'^(.*?)media/(?P<path>.*)$', django.views.static.serve,
{'document_root': settings.MEDIA_ROOT}),
]
Step 3: Now I created an onclick in the HTML tag as:
My pdf files were in documents folder inside the media directory
onclick="window.open('media/documents/{{Your/Pdf/File/Path.pdf}}#page={{Page_number_to_open}}')"
Related
I have set up a basic Django system and am testing out the admin app. The admin screen displays with raw styles and not the pretty ones shown in the tutorials and specified in the style-sheets.
Following several other questions, I found this is a common problem caused because Django doesn't normally serve static files. However, it does if DEBUG is switched on and I do have debug switched on. I followed all of the suggestions in the answers anyway to collect static files, etc.
If I enter the static file URLs directly they get downloaded and I can see the files in the developer mode of the browser (both Chrome and Edge). So I think the static files are being served.
Furthermore, if I save the page using the browser, it saves all of the static files and then if I open the main page it is shown with the correct styles. So the styles are working.
So it would seem to me that the files are being served and the browser is getting them and it can render them (from a saved file) but it somehow isn't working when served from Django.
Can anyone tell me what is going on?
EDIT:
Here is a strange thing: if, using the Chrome developer tool, I select the base.css file, click somewhere in the text of the CSS (say at the end of a line) and then add a space, suddenly the page appears correctly.
If I then do refresh the page it goes back to unstyled again.
EDIT 2:
I saw a recommendation to install the Whitenoise app to serve static file so I went ahead and did it. I turned off debug and presto! the styles appear. Turning on debug (so I presume django serves the files) and the styles go away. I saved both sites to the file system and compared the directories using a compare tool. There was no difference.
I'm not calling this an answer as the question is:
Why?
Also, I can't have debug on and get styles.
I am writing a small Django local project (with no access to internet). Let's say i have offline standalone version of Wikipedia(I believe it's HTML5 format) as that: .
I am trying to write a simple Django front page where user would be able to click a button and be redirected to "wikipedia_for_schools/index.html" from where all the url control will be done by that offline wikipedia stand alone page.
How is it possible to make? :/ I have tried creating a link in django template as
Click to see wikipedia
but that doesn't work cause Django complains that "http://172.30.10.67:8000/modules/wikipedia_for_schools/index.htm" Page not found, my urls.py is just
urlpatterns = [
url(r'^$', views.IndexView, name='index'),
and i think it's impossible to rewrite each file from wikipedia offline project to Django MVT model to provide urls, models and templates for each link.
That's how my Django project structure looks like:
And that's how offline Wikipedia index page looks
Any help would be highly appreciated
Standalone HTML pages are static files, and should be stored in the static folder and accessed via /static/.
The problem is described in title. I have this template for a blog that I'm creating using Django.
When I open it normally, using double click over the HTML file, it looks like this:
But when I open it via url from the Django project, it looks like this
It only shows a green square (part of the css) but obviously can't open the css correctly.
Any idea to solve this?
In Django you don't open the HTML with double click on the file, you need to run the server first and open your site using the localhost (like you did in the second picture).
Judging by those images, are you sure you put the image in the static folder? In Django, the HTML files stays in the "templates" folder of your app and the css, javascript and images in the "static" folder.
If this answer doesn't help you, then you should post your code here, otherwise I can't find the problem.
Now I have a django project and some small html project (in fact, some html5 games). How can I added the html site to the django project?
For the each html project has ref a lot of css, img and javascript in its own folder, and different html site are in different folder.
Hence, I can not now just do like this:
t = get_template('htmlprojectfolder/index.html')
html = t.render(Context())
return HttpResponse(html)
I think my problem is:
"how can I jump to a html index page with its static files correct linked?" (Clealy, I can not read just the index page as a template file.)
Thank you for help!
I think the best way to add static, non-Django files that shall be accessible via a Django project would be to add the files to the STATICFILES_DIR. You can read more about static files here in the Django documentation.
Another, and possibly better, way to handle this would be to let the webserver serve the static files separately from Django.
After using atreal.richfile.preview in plone, we get the preview of a pdf file with the url like : http://localhost:8090/plone/sample.pdf/view. If we delete part of the url i.e "/view", and enter the url: http://localhost:8090/plone/sample.pdf in the browser, it still can be viewed and the pdf becomes printable or can be copied. How can I modify the url so that it will not display the pdf in the new window if the url of the same is modified? Using plone 4.1. Which template and what code needs to be added/ edited. Please guide
In the example above, sample.pdf is presumably a File created in Plone. In this case, the URL without /view will render the file for download, and the URL with /view will render a Plone page with a link to download it. This is standard behaviour.
It isn't really possible to stop people from downloading the PDF. You can modify the File FTI in portal_types (go to portal_types/File in the ZMI and change the method aliases tab). If you change the "(Default)" alias to be the same as the "view" one, it will behave the same. Note that this will affect all files.
Martin