35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import {useApi} from '~/composables/useApi'
|
|
|
|
export const usePdfPrinter = () => {
|
|
const api = useApi()
|
|
const receptionStore = useReceptionStore()
|
|
const currentReception = receptionStore.current
|
|
|
|
const printPdf = async (url: string): Promise<void> => {
|
|
const blob = await api.getBlob(url);
|
|
|
|
const pdfBlob = blob.type === 'application/pdf'
|
|
? blob
|
|
: new Blob([blob], { type: 'application/pdf' });
|
|
|
|
const blobUrl = URL.createObjectURL(pdfBlob);
|
|
|
|
const filename = `${currentReception.identificationNumber}_${currentReception.supplier.name}_${currentReception.licensePlate}.pdf`;
|
|
|
|
const a = document.createElement('a');
|
|
a.href = blobUrl;
|
|
a.download = filename;
|
|
a.style.display = 'none';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
|
|
window.open(blobUrl, '_blank', 'noopener,noreferrer');
|
|
setTimeout(() => URL.revokeObjectURL(blobUrl), 60_000);
|
|
}
|
|
|
|
return {
|
|
printPdf
|
|
}
|
|
}
|