Skip to content
Snippets Groups Projects
Commit 37a48960 authored by Thomas Schnaak's avatar Thomas Schnaak
Browse files

feat: added download for pdf export

parent 5367eaa0
No related branches found
No related tags found
No related merge requests found
package unipotsdam.gf.modules.journal.view;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import unipotsdam.gf.interfaces.IJournal;
import unipotsdam.gf.modules.journal.model.EPortfolio;
import unipotsdam.gf.modules.journal.service.IJournalImpl;
import unipotsdam.gf.modules.journal.service.JournalService;
import unipotsdam.gf.modules.journal.service.JournalServiceImpl;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
/**
* View for exporting the EPortfolio
......@@ -23,21 +23,33 @@ import javax.ws.rs.core.Response;
public class EPortfolioView {
private final Logger log = LoggerFactory.getLogger(EPortfolioView.class);
private final JournalService journalService = new JournalServiceImpl();
/**
* Returns the pdf file for export
* TODO use owncloud when implemented
* @param student ID of student
* @param project ID of project
* @return pdf
*/
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Produces("application/pdf")
@Path("/pdf/{student}/{project}")
public Response getPdf (@PathParam("student") String student, @PathParam("project") String project){
public Response getPdfB (@PathParam("student") String student, @PathParam("project") String project){
IJournal iJournal = new IJournalImpl();
File file = new File("portfolio.pdf");
Response.ResponseBuilder response = Response.ok(file);
EPortfolio ePortfolio = iJournal.getPortfolio(project,student);
byte[] pdf = iJournal.exportPortfolioToPdf(ePortfolio);
try {
FileUtils.writeByteArrayToFile(file, iJournal.exportPortfolioToPdf(ePortfolio));
} catch (IOException e) {
e.printStackTrace();
}
return Response.ok(pdf).build();
response.header("Content-Disposition", "filename=restfile.pdf");
return response.build();
}
}
......@@ -48,11 +48,11 @@
<a id="editDescriptionLink" class="btn btn-default btn-sm">
<i class="fa fa-pencil"></i> Bearbeiten</a>
<a class="btn btn-default btn-sm" data-toggle="modal" data-target="#closeDescriptionModal"><i class="fa fa-check-square" aria-hidden="true"></i>Abschlie&szlig;en</a>
<!-- TODO: find place for Button -->
<button type="button" class="btn btn-primary mr-auto" data-dismiss="modal" onclick="downloadPortfolio()">
EPortfolio heurunterladen
</button>
</div>
<!-- TODO: Variabel -->
<div class="exportLink">
</div>
</div>
<div class="journal-description-text">
</div>
<div class="journal-description-group">
......
......@@ -24,8 +24,7 @@ $(document).ready(function () {
$('.journal-description-group').append(data.group[g] + '<br/>');
}
$('.exportLink').append('<a class="btn btn-default btn-sm" href="../rest/eportfolio/pdf/'+student +'/' + project + '">Portfolio herunterladen</a>');
console.log(data);
});
......@@ -165,37 +164,18 @@ function closeDescription() {
}
//load PDF via rest
//source: https://stackoverflow.com/questions/41803925/download-octet-stream-via-jquery
function downloadPortfolio() {
const saveData = (() => {
const a = document.createElement('a');
a.style = 'display: none';
document.body.appendChild(a);
return (data, fileName, type = 'octet/stream') => {
console.log(data);
const blob = new Blob([data], { type });
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(blob, fileName);
}
$.ajax({
type: "GET",
url: "../rest/eportfolio/pdfB/" + student + "/" + project,
dataType: "application/pdf",
success: function(data, textStatus, jqXHR) {
window.open("data:application/pdf," + escape(data));
},
});
const url = URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
URL.revokeObjectURL(url);
return true;
};
})();
$.ajax({
method: "GET",
contentType: "application/x-www-form-urlencoded",
url: "../rest/eportfolio/pdf/" + student + "/" + project,
})
.done((data) => saveData(data, 'portfolio.pdf'));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment