Bouton d'export dans l'interface + nommage du pdf

This commit is contained in:
Teddy Cornaut 2020-06-15 15:31:22 -04:00
parent 6ebe4c90d7
commit 47020f7beb
2 changed files with 40 additions and 11 deletions

View File

@ -12,15 +12,50 @@ const ExportTab: FunctionalComponent<ExportTabProps> = ({ project }) => {
route(`/pdf/${uuid}`);
};
const showFile = (blob:any) => {
var newBlob = new Blob([blob], {type: "application/pdf"})
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(newBlob);
return;
}
const data = window.URL.createObjectURL(newBlob);
var link = document.createElement('a');
link.href = data;
link.download=project.label+".pdf";
link.click();
setTimeout(function(){
window.URL.revokeObjectURL(data);
}, 100);
}
const options = {
projectURL: `//${window.location.host}/export/projects`,
}
const exportPdf = () => {
return fetch(`${options.projectURL}/${project.id}`, {
method: 'GET',
}).then(r => r.blob())
.then(showFile);
};
return (
<div>
<label class="label is-size-4">Format PDF</label>
<div class="field">
<button class="button is-primary"
<button class="button is-default"
onClick={displayPdf}>
Aperçu PDF
</button>
</div>
<div class="field">
<a class="button is-primary"
onClick={exportPdf}>
Exporter PDF
</a>
</div>
<hr />
<label class="label is-size-4">Format JSON</label>
<pre>{ JSON.stringify(project, null, 2) }</pre>

View File

@ -119,12 +119,6 @@ func handleExportProject(w http.ResponseWriter, r *http.Request) {
log.Fatal(err)
}
// Write buffer contents to file on disk
err = pdfg.WriteFile("./sample.pdf")
if err != nil {
log.Fatal(err)
}
rsp, err := writePDF(w, http.StatusOK, pdfg.Bytes())
if err != nil {
panic(errors.Wrap(err, "could not write pdf response"))
@ -307,7 +301,7 @@ func writeJSON(w http.ResponseWriter, statusCode int, data interface{}) error {
}
func writePDF(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
w.Header().Set("Content-Disposition", "attachment; filename=foo.pdf")
w.Header().Set("Content-Disposition", "attachment; filename=estimation.pdf")
w.Header().Set("Content-Type", "application/pdf")
w.WriteHeader(statusCode)