Bouton d'export dans l'interface + nommage du pdf
This commit is contained in:
parent
6ebe4c90d7
commit
47020f7beb
|
@ -12,14 +12,49 @@ const ExportTab: FunctionalComponent<ExportTabProps> = ({ project }) => {
|
||||||
route(`/pdf/${uuid}`);
|
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 (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<label class="label is-size-4">Format PDF</label>
|
<label class="label is-size-4">Format PDF</label>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<button class="button is-primary"
|
<button class="button is-default"
|
||||||
onClick={displayPdf}>
|
onClick={displayPdf}>
|
||||||
Aperçu PDF
|
Aperçu PDF
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<a class="button is-primary"
|
||||||
|
onClick={exportPdf}>
|
||||||
|
Exporter PDF
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<hr />
|
<hr />
|
||||||
<label class="label is-size-4">Format JSON</label>
|
<label class="label is-size-4">Format JSON</label>
|
||||||
|
|
|
@ -119,12 +119,6 @@ func handleExportProject(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Fatal(err)
|
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())
|
rsp, err := writePDF(w, http.StatusOK, pdfg.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(errors.Wrap(err, "could not write pdf response"))
|
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) {
|
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.Header().Set("Content-Type", "application/pdf")
|
||||||
|
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
|
|
Loading…
Reference in New Issue