mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-03 21:37:49 -05:00
WIP pdf embedding
This commit is contained in:
@@ -41,7 +41,7 @@
|
||||
{% for r in b.recipes %}
|
||||
<div class="row">
|
||||
<div class="col col-md-10">
|
||||
<li><a href="#" onClick='openRecipe({{ r.recipe.pk }})'>{{ r.recipe.name }}</a></li>
|
||||
<li><a href="{% url 'view_recipe' r.recipe.pk %}">{{ r.recipe.name }}</a></li>
|
||||
</div>
|
||||
<div class="col col-md-2" style="text-align: right">
|
||||
<a href="{% url 'delete_recipe_book_entry' r.pk %}"><i class="fas fa-trash-alt"></i></a>
|
||||
@@ -58,5 +58,4 @@
|
||||
<br/>
|
||||
{% endfor %}
|
||||
|
||||
{% include 'include/recipe_open_modal.html' %}
|
||||
{% endblock %}
|
||||
@@ -43,15 +43,12 @@
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
function openRecipe(id, force_external = false) {
|
||||
function openRecipe(id) {
|
||||
var link = $('#a_recipe_open');
|
||||
link.hide();
|
||||
$('#div_loader').show();
|
||||
|
||||
var url = "{% url 'api_get_file_link' recipe_id=12345 %}".replace(/12345/, id);
|
||||
if (force_external) {
|
||||
url = "{% url 'api_get_external_file_link' recipe_id=12345 %}".replace(/12345/, id);
|
||||
}
|
||||
var url = "{% url 'api_get_external_file_link' recipe_id=12345 %}".replace(/12345/, id);
|
||||
|
||||
link.text("{% trans 'Open Recipe' %}");
|
||||
$('#modal_recipe').modal('show');
|
||||
|
||||
@@ -63,6 +63,4 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% include 'include/recipe_open_modal.html' %}
|
||||
|
||||
{% endblock %}
|
||||
@@ -56,7 +56,7 @@
|
||||
<td>
|
||||
{% for mp in days_value %}
|
||||
<a href="{% url 'edit_plan' mp.pk %}"><i class="fas fa-edit"></i></a>
|
||||
<a href="#" onclick="openRecipe({{ mp.recipe.id }})">{{ mp.recipe.name }}</a><br/>
|
||||
<a href="{% url 'view_recipe' mp.recipe.id %}">{{ mp.recipe.name }}</a><br/>
|
||||
{% endfor %}
|
||||
</td>
|
||||
{% endfor %}
|
||||
@@ -67,6 +67,4 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% include 'include/recipe_open_modal.html' %}
|
||||
|
||||
{% endblock %}
|
||||
@@ -134,84 +134,96 @@
|
||||
{% if recipe.storage %}
|
||||
<div class="row">
|
||||
{% if recipe.internal %}
|
||||
<a href='#' onClick='openRecipe({{ recipe.id }}, true)'
|
||||
<a href='#' onClick='openRecipe({{ recipe.id }})'
|
||||
class="d-print-none">{% trans 'View external recipe' %} <i class="fas fa-external-link-alt"></i></a>
|
||||
{% else %}
|
||||
|
||||
<canvas id="id_pdf_canvas" class="border"></canvas>
|
||||
<div class="col col-md-12" style="margin-top: 2vh">
|
||||
<div class="loader" id="id_loader"></div>
|
||||
<canvas id="id_pdf_canvas" class="border"></canvas>
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
<br/>
|
||||
<div class="card border-info">
|
||||
<div class="card-body text-info">
|
||||
<h5 class="card-title">{% trans 'External recipe' %}</h5>
|
||||
<p class="card-text">
|
||||
{% blocktrans %}
|
||||
This is an external recipe, which means you can only view it by opening the link above.
|
||||
You can convert this recipe to a fancy recipe by pressing the convert button. The
|
||||
original
|
||||
file
|
||||
will still be accessible.
|
||||
{% endblocktrans %}.
|
||||
<br/>
|
||||
<br/>
|
||||
<a href="{% url 'edit_convert_recipe' recipe.pk %}"
|
||||
class="card-link btn btn-info">{% trans 'Convert now!' %}</a>
|
||||
</p>
|
||||
<div class="col col-md-12" style="margin-top: 2vh">
|
||||
<div class="card border-info">
|
||||
<div class="card-body text-info">
|
||||
<h5 class="card-title">{% trans 'External recipe' %}</h5>
|
||||
<p class="card-text">
|
||||
{% blocktrans %}
|
||||
This is an external recipe, which means you can only view it by opening the link
|
||||
above.
|
||||
You can convert this recipe to a fancy recipe by pressing the convert button. The
|
||||
original
|
||||
file
|
||||
will still be accessible.
|
||||
{% endblocktrans %}.
|
||||
<br/>
|
||||
<br/>
|
||||
<a href="{% url 'edit_convert_recipe' recipe.pk %}"
|
||||
class="card-link btn btn-info">{% trans 'Convert now!' %}</a>
|
||||
<a href='#' onClick='openRecipe({{ recipe.id }})'
|
||||
class="d-print-none btn btn-warning">{% trans 'View external recipe' %} <i
|
||||
class="fas fa-external-link-alt"></i></a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.3.200/pdf.min.js"
|
||||
integrity="sha256-J4Z8Fhj2MITUakMQatkqOVdtqodUlwHtQ/ey6fSsudE="
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
var url = "{% url 'api_get_external_file_link' recipe_id=12345 %}".replace(/12345/, {{ recipe.id }});
|
||||
|
||||
var url = "{% url 'api_get_cors_file_link' recipe_id=12345 %}".replace(/12345/, {{ recipe.id }});
|
||||
$('#id_pdf_canvas').hide();
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function () {
|
||||
if (this.readyState === 4 && this.status === 200) {
|
||||
var url = this.responseText;
|
||||
$('#id_loader').hide();
|
||||
|
||||
var loadingTask = pdfjsLib.getDocument(url.replace('www.dropbox.', 'dl.dropboxusercontent.'));
|
||||
loadingTask.promise.then(function (pdf) {
|
||||
console.log('PDF loaded');
|
||||
if (url === "None") {
|
||||
// direct previews are not supported for this provider
|
||||
//TODO implement something useful for providers not allowing cors links
|
||||
} else {
|
||||
var loadingTask = pdfjsLib.getDocument(url);
|
||||
loadingTask.promise.then(function (pdf) {
|
||||
$('#id_pdf_canvas').show();
|
||||
|
||||
// Fetch the first page
|
||||
var pageNumber = 1;
|
||||
pdf.getPage(pageNumber).then(function (page) {
|
||||
console.log('Page loaded');
|
||||
// Fetch the first page
|
||||
var pageNumber = 1;
|
||||
pdf.getPage(pageNumber).then(function (page) {
|
||||
|
||||
var scale = 1.5;
|
||||
var viewport = page.getViewport({scale: scale*0.8});
|
||||
|
||||
// Prepare canvas using PDF page dimensions
|
||||
var canvas = document.getElementById('id_pdf_canvas');
|
||||
var context = canvas.getContext('2d');
|
||||
canvas.height = viewport.height;
|
||||
canvas.width = viewport.width;
|
||||
var scale = 1.5;
|
||||
var viewport = page.getViewport({scale: scale * 0.8});
|
||||
|
||||
// Render PDF page into canvas context
|
||||
var renderContext = {
|
||||
canvasContext: context,
|
||||
viewport: viewport
|
||||
};
|
||||
var renderTask = page.render(renderContext);
|
||||
renderTask.promise.then(function () {
|
||||
console.log('Page rendered');
|
||||
// Prepare canvas using PDF page dimensions
|
||||
var canvas = document.getElementById('id_pdf_canvas');
|
||||
var context = canvas.getContext('2d');
|
||||
canvas.height = viewport.height;
|
||||
canvas.width = viewport.width;
|
||||
|
||||
// Render PDF page into canvas context
|
||||
var renderContext = {
|
||||
canvasContext: context,
|
||||
viewport: viewport
|
||||
};
|
||||
var renderTask = page.render(renderContext);
|
||||
renderTask.promise.then(function () {
|
||||
console.log('Page rendered');
|
||||
});
|
||||
});
|
||||
}, function (reason) {
|
||||
// PDF loading error
|
||||
console.error(reason);
|
||||
});
|
||||
}, function (reason) {
|
||||
// PDF loading error
|
||||
console.error(reason);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
xhttp.open("GET", url, true);
|
||||
xhttp.send();
|
||||
|
||||
|
||||
</script>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user