Displaying a PDF from the Blobstore - django

I have various PDF documents as blobs in the App Engine blobstore. Now, I want to preview them on a HTML page. From various other questions, I figured the HTML setup should be:
<embed src="{{ url_to_my_pdf_blob }}" width="500" height="600">
While experimenting, I found that in the SDK the get_serving_url function will not only work for images but will also work for PDF blobs. This approach unfortunately fails in production.
I looked at the example using blobstore_handlers.BlobstoreDownloadHandler but could not figure out how to wire this to my view (my app is based on Django, the HTML file with the preview is served by a Django view class).
How do I get the url of the blob?

You need to define a ServeHandler, as described here:
https://developers.google.com/appengine/docs/python/blobstore/#Python_Serving_a_blob
For examples of the implementation, see Downloaded filename with Google App Engine Blobstore.

Related

How to render a pdf file as pdf in browser using django

I am using Django web frame work. I am taking pdf files from users (notes/book/etc). Then I want to display those pdf in website, but I could not find a way. Methods that failed 1)iframe 2)simple rendering using django 3)pdf.js library. Also browser blocks rendering initially. Care to suggest a method and steps to implement it.
I the answer to your problem is here :
https://docs.djangoproject.com/en/3.0/howto/outputting-pdf/
Well the answer has to be very very clear here. You can not display pdf using. There is no such library for that. Like pdf.js for javascript based frameworks. I have done the research. So if you want a really good pdf viewer for your django powered website you have to use pdf.js for sure. I have asked people working with django professionally.
output in firefox
ouput in chromium browser
Now 2nd output is what we want
here is the code

Django - how to render all pages of pdf files at once

I am trying to make a pdf viewer in HTML template using pdf.js in Django framework.
I had used a code that I got from a Youtube channel Traversy Media on how to create a custom PDF Viewer With JavaScript. It is working but I want to know if it's possible to render all pages at once.
Here's the link of the pdf.js full code in GitHub...
Is there an alternative way rather than sticking to this code?

Editing a previously generated HTML file with Django

I am very new to web development and the following is my use-case :
I have a large number of Bokeh charts, each in a separate HTML file.
In simple terms, I would like to have a home page, where I can provide
links to each one of these charts. However, During runtime, I would
like to edit these separate HTML files, so as to provide a link to go
back to the home page or to other pages. I would not like to modify
the HTML files permanently, so I can make use of them outside of the
web page as well for simple visualization on my system.
What is the best way to do this ? Are there technologies outside Django, I should be looking at to do something like this ?
If most of the content is static, maybe have a look at Jekyll.
The include functionality would let you create one file with the 'link back to the homepage' or in fact further content which you want to avoid repeating (such as navbars, headers, footers).
Bootstrap 4 is your freind for making the site look shiney.
As you're building the site you can run the development server with jekyll serve which allows you to connect to a development server from your browser, and preview changes as you're making them. This would be accessible somewhere like http://localhost:4000/
When you're ready to publish, you can use the jekyll build command, which outputs all of the static HTML files to the _site directory. Notice that at this point, the step of 'putting the homepage link in every page' is handled automatically by Jekyll and you end up with a directory you can upload directly to any hosting platform. The original HTML files/Boken Charts can therefor remain in their original form for use elsewhere.
This method is probably much more effiient than using Django for your use-case, which seems to require serving lots of static content whch already exists. With Django in production you'd need an application server, as well as a webserver and possibly a database which means more things to go wrong.
For bonus points, once you've got the hosting setup, stick the whole thing behind CloudFlare to reduce your hosting costs, and improve access speeds for visitors around the world!
Good luck.
EDIT: response to comment:*
Do you mean that I should abandon django altogether ?
If the purpose is just to serve your exising HTML files to the public, without any requirement for authentication, editing of content by users through the frontend, or more advanced back-end functions, then yes Django is probably overkill for this task.
How is Jekyll different from Django ?
Django is a Python Web Framework, which allows you to build an interactive site on which users or staff can login, post articles, comment, etc. One of its key features is the ability to define database relationships trough 'Models' and then have all the admin-side forms generated automatically in the background. This means, with minimal work, you can instantly have the 'admin portal' side of the site live, which works great for use-cases like large blogs or news sites. You would then build the frontend, which can also be interactive. To launch this into production is a separate task which involves configuring multiple server components.
Jeykll on the other hand is much simpler, and basically gives you a way to create some template HTML files (avoiding the need to repeat code for stuff like navigation bars) and then with the jekyll build command outputs a _site directory which can be uploaded straight to a basic webserver. This is the crucial part, as you then only need a webserver which can serve static content, rather than requiring python, a database, application server like UWSGI, etc
Let's look at this example from the Jeyll Docs with your usecase in mind.
You could define a YML file with a list of all your charts:
docs_list_title: All Charts here.
docs:
- title: A Lovely Bokeh Chart.
url: bokeh_chart_1.html
- title: This Bokeh Chart is even Better
url: bokeh_chart_2.html
You mentioned previously that you already have the HTML files, so really what you've done here is made a list of those, which can be interpreted by the frontend.
The HTML template portion would look something like this:
<h2>{{ site.data.samplelist.docs_list_title }}</h2>
<ul>
{% for item in site.data.samplelist.docs %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
This would result in a list of links to all of your Charts, with the link text as the title.
Obviously you could then go further and add further info to the YML file, like beneath each url put publisher: someone which could then be accessed in the template's for loop as {{item.publisher}}
Can such tools like Jekyll, Django and Bootstrap be used together ?
Bootstrap can be used with Django or Jekyll, as it is a CSS library which controls how HTML is rendered in the user's browser. Check the documentation for more examples of its capability.
A good starting point may be to download a theme from somewhere like Start Bootstrap. Once you have that as a ZIP file, you can put it in your Jeykll project and attempt to have it render through the dev server with jekyll serve. You can then remove nav bar or header code to separate include files (see my earlier link to the Jeykll docs) and before you know it you'll be seeing progress.
The best way to learn is to just go ahead and try this!

How to Embed a Google Doc Chart in Joomla

How do I embed a Google Docs chart in a Joomla 2.5 custom HTML module? When I publish a chart and receive the "script" I paste it into a module and then the editor seems to strip the code out. I have tried 4 different editors and also "no editor" and it still modifies the script. Can someone help?
Google Docs offers an undocumented feature that lets you embed PDF files and PowerPoint presentations in a web page. The files don't have to be uploaded to Google Docs, but they need to be available online.
Here's the code I used to embed the PDF file:
<iframe src="http://docs.google.com/gview?url=http://www.uom.mx/programas/Kinam%20programa.pdf&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>
but you should replace the bold URL with your own address. As I mentioned, the document viewer works for PDF and PPT files.

django sorl-thumbnail json

I am using Sorl-Thumbnail to generate thumbnails within pages via template tags.
This all works perfectly fine.
However I have now added a store locator function to the site which feeds into my Google Maps JS via JSON.
In the infowindow popups I would like to put a tiny image of the storefront.
So I need to create another size of thumbnail and I need to include the URL for this within my JSON.
It seemed initially that Sorl doesnt support this due to its requirement for Tags within a page.
I have looked at the low level API version "get_thumbnail" and have tried to implement this within my Model without much luck so far.
I have no problem with my JSON and serialisation, just getting the thumbnail generated and into the model prior to this.
Has anyone had any experience with this scenario previously?
Cheers
Kev