@movalib/movalib-commons 1.55.5 → 1.55.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -179,8 +179,35 @@ export class PLVComponent extends React.Component<PLVComponentProps> {
179
179
  }
180
180
  }
181
181
 
182
+
183
+
184
+ export class PLVComponentV2 extends React.Component<PLVComponentProps> {
185
+ render() {
186
+ return (
187
+ <div
188
+ className='printable-content'
189
+ style={{
190
+ width:
191
+ this.props.printSize === PrintSize.A4 ? '210mm' : this.props.printSize === PrintSize.A3 ? '297mm' : '0mm',
192
+ height:
193
+ this.props.printSize === PrintSize.A4 ? '297mm' : this.props.printSize === PrintSize.A3 ? '420mm' : '0mm',
194
+ }}
195
+ >
196
+ <Grid container>
197
+ <Grid item xs={12}>
198
+ <img src={this.props.url} style={{ display: 'block', width: '100%', height: '100%', objectFit: 'cover' }} />
199
+ </Grid>
200
+ </Grid>
201
+ </div>
202
+ );
203
+ }
204
+ }
205
+
182
206
  // CSS pour cacher le contenu à l'écran mais le rendre imprimable
183
207
  const css = `
208
+ body {
209
+ margin: 0;
210
+ }
184
211
  .highlight {
185
212
  background-color: #BCD46C; /* Couleur de surlignage */
186
213
  margin: 4px 4px; /* Espacement autour du texte surligné */
@@ -197,13 +224,21 @@ const css = `
197
224
  }
198
225
 
199
226
  .printable-content {
227
+ margin: 0;
228
+ padding: 0;
200
229
  visibility: hidden;
201
230
  position: absolute;
202
231
  left: -10000px;
203
232
  top: -10000px;
233
+
204
234
  }
205
235
  @media print {
236
+ body {
237
+ margin: 0;
238
+ }
206
239
  .printable-content {
240
+ margin: 0;
241
+ padding: 0;
207
242
  visibility: visible;
208
243
  position: static;
209
244
  }
@@ -20,21 +20,23 @@ import { useReactToPrint } from 'react-to-print';
20
20
  import { DownloadedQRCode } from '../../DownloadedQRCode';
21
21
  import QRCodeImage from '../../assets/images/leaf_yellow_small.png';
22
22
  import { flexCenter } from '../../helpers/Tools';
23
- import { PLVComponent, PrintSize } from './PLVComponent';
24
23
  import { PDFDocument } from 'pdf-lib';
25
- import { openDialogPrint } from '../../utils/DialogPrint';
24
+ import { PLVComponentV2, PrintSize } from './PLVComponent';
25
+ import * as pdfjsLib from 'pdfjs-dist';
26
+ pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.mjs';
26
27
 
27
28
  type SelectChoice = null | 'A3' | 'A4' | 'QR_Headless' | 'QR_Google';
28
-
29
29
  export const QrCodePLVContainer = ({ data }: { data: string }) => {
30
30
  const [selectedChoice, setSelectedChoice] = useState<SelectChoice>(null);
31
+ const [urlA4, setUrlA4] = useState<string>('');
32
+ const [urlA3, setUrlA3] = useState<string>('');
31
33
 
32
34
  const qrCodeRef = useRef<HTMLDivElement>(null);
33
35
  const theme = useTheme();
34
-
36
+ const PLVrefA4 = useRef<PLVComponentV2>(null);
37
+ const PLVrefA3 = useRef<PLVComponentV2>(null);
35
38
  //si sur l'impression des PLV en A4 ou A3 le Qrcode est flou,
36
39
  //il faut modifier la width et height, imageSize du qrCodeOptions et mettre les mêmes valeurs que dans le optionQrcode de la fonction printPLV
37
- //
38
40
  const qrCodeOptions: Options = useMemo(
39
41
  () => ({
40
42
  width: 300,
@@ -79,6 +81,16 @@ export const QrCodePLVContainer = ({ data }: { data: string }) => {
79
81
  qrCode.append(qrCodeRef.current);
80
82
  }
81
83
  }, [qrCode]);
84
+
85
+ useEffect(() => {
86
+ const urlA4 = printPLV('A4').then((url) => {
87
+ setUrlA4(url);
88
+ });
89
+ const urlA3 = printPLV('A3').then((url) => {
90
+ setUrlA3(url);
91
+ });
92
+ }, [data]);
93
+
82
94
  async function printPLV(format: string) {
83
95
  // Chemin du fichier PDF
84
96
  const pdfUrl = `/Movalib_${format}.pdf`;
@@ -97,7 +109,7 @@ export const QrCodePLVContainer = ({ data }: { data: string }) => {
97
109
  const qrImage = await pdfDoc.embedPng(qrImageBytes!);
98
110
 
99
111
  const pages = pdfDoc.getPages();
100
- const firstPage = pages[0]; // Ajouter le QR code à la première page
112
+ const firstPage = pages[0];
101
113
  const { width, height } = firstPage.getSize();
102
114
 
103
115
  let optionQrcode = {};
@@ -126,8 +138,38 @@ export const QrCodePLVContainer = ({ data }: { data: string }) => {
126
138
  const blob = new Blob([modifiedPdfBytes], { type: 'application/pdf' });
127
139
  const url = URL.createObjectURL(blob);
128
140
 
129
- // Ouvrir la page d'impression
130
- openDialogPrint(url);
141
+ // étape nécesssaire pour utiliser reactToPrint pour pouvoir nommer le document
142
+ // reactToPrint fonctionne uniquement avec le HTML
143
+ // donc il faut convertir le PDF en image
144
+ // puis convertir l'image en URL
145
+ // et enfin integrer l'image dans le HTML via la balise <img>
146
+
147
+
148
+ // Convertir le PDF en image
149
+ const loadingTask = pdfjsLib.getDocument(url);
150
+ const pdf = await loadingTask.promise;
151
+
152
+ // Récupérer la première page
153
+ const page = await pdf.getPage(1);
154
+
155
+ // Créer un canvas pour afficher la page
156
+ const canvas = document.createElement('canvas');
157
+ const context = canvas.getContext('2d');
158
+
159
+ const viewport = page.getViewport({ scale: 1.5 });
160
+ canvas.width = viewport.width;
161
+ canvas.height = viewport.height;
162
+
163
+ // Rendu de la page dans le canvas
164
+ const renderContext = {
165
+ canvasContext: context!,
166
+ viewport: viewport,
167
+ };
168
+ await page.render(renderContext).promise;
169
+
170
+ // Convertir le canvas en URL d'image (data URL)
171
+ const imageUrl = canvas.toDataURL('image/png');
172
+ return imageUrl;
131
173
  }
132
174
 
133
175
  const downloadQRCodeHeadless = useCallback(() => {
@@ -176,13 +218,23 @@ export const QrCodePLVContainer = ({ data }: { data: string }) => {
176
218
  const onChangeSelectedChoice: SelectProps<SelectChoice>['onChange'] = (e) => {
177
219
  setSelectedChoice(e.target.value as SelectChoice);
178
220
  };
221
+
222
+ const printA3PLV = useReactToPrint({
223
+ content: () => PLVrefA3.current,
224
+ documentTitle: 'Movalib_PLV_A3',
225
+ });
226
+
227
+ const printA4PLV = useReactToPrint({
228
+ content: () => PLVrefA4.current,
229
+ documentTitle: 'Movalib_PLV_A4',
230
+ });
179
231
 
180
232
  const onClickDownload = useMemo(() => {
181
233
  switch (selectedChoice) {
182
234
  case 'A3':
183
- return () => {printPLV('A3')};
235
+ return printA3PLV;
184
236
  case 'A4':
185
- return () => {printPLV('A4')};
237
+ return printA4PLV;
186
238
  case 'QR_Headless':
187
239
  return downloadQRCodeHeadless;
188
240
  case 'QR_Google':
@@ -190,9 +242,12 @@ export const QrCodePLVContainer = ({ data }: { data: string }) => {
190
242
  default:
191
243
  return () => {};
192
244
  }
193
- }, [selectedChoice, downloadQRCodeHeadless, downloadQrCodeWithCTA]);
245
+ }, [selectedChoice, downloadQRCodeHeadless, downloadQrCodeWithCTA, printA3PLV, printA4PLV]);
194
246
  return (
195
247
  <Box display='flex' flexDirection='column' alignItems='center' gap='24px'>
248
+ {urlA4 && <PLVComponentV2 ref={PLVrefA4} url={urlA4} printSize={PrintSize.A4} />}
249
+ {urlA3 &&<PLVComponentV2 ref={PLVrefA3} url={urlA3} printSize={PrintSize.A3} />}
250
+
196
251
  <Grid container justifyContent='center' alignItems='center'>
197
252
  <Grid item xs={7}>
198
253
  <FormControl fullWidth size='small'>
@@ -7,7 +7,6 @@ export const API_BASE_URL = process.env.REACT_APP_API_URL || 'https://localhost:
7
7
  type APISuccess<T> = {
8
8
  success: true;
9
9
  data: T;
10
- location: string
11
10
  };
12
11
 
13
12
  type APIError = {
@@ -21,8 +20,8 @@ type APIError = {
21
20
  export type APIResponse<T> = {
22
21
  success: boolean,
23
22
  data?: T,
24
- error?: string,
25
- location?: string | null
23
+ error?: string
24
+
26
25
  }
27
26
 
28
27
  export type APIRequest = {
@@ -36,7 +35,7 @@ function handleError(error: Error): APIResponse<null> {
36
35
  let msg = error.message.includes('fetch') ? "Connexion impossible" : error.message;
37
36
  return {
38
37
  success: false,
39
- error: msg,
38
+ error: msg
40
39
  };
41
40
  }
42
41
 
@@ -45,8 +44,6 @@ function handleResponse(response: Response): Promise<APIResponse<any>> {
45
44
  const contentType = response.headers.get("content-type");
46
45
  const isJson = contentType && contentType.includes("application/json");
47
46
  const dataPromise = isJson ? response.json() : response.text();
48
- const location = response.headers.get('Location');
49
-
50
47
 
51
48
  return dataPromise.then(data => {
52
49
 
@@ -70,7 +67,7 @@ function handleResponse(response: Response): Promise<APIResponse<any>> {
70
67
  return { success: false, error: errorMsg };
71
68
  }
72
69
 
73
- return { success: true, data, location: location };
70
+ return { success: true, data };
74
71
  });
75
72
  }
76
73
 
@@ -1,4 +1,5 @@
1
1
  export function openDialogPrint(url: string) {
2
+ // Créer un iframe pour charger le document Blob => url
2
3
  const iframe = document.createElement('iframe');
3
4
  iframe.style.position = 'absolute';
4
5
  iframe.style.width = '0px';
@@ -10,7 +11,7 @@ export function openDialogPrint(url: string) {
10
11
  iframe.onload = () => {
11
12
  iframe?.contentWindow?.print();
12
13
  };
13
- // Supprimer l'iframe une fois l'impression terminée
14
+ // Supprimer l'iframe une fois lque la dialog est fermer
14
15
  iframe.onclose = () => {
15
16
  document.body.removeChild(iframe);
16
17
  URL.revokeObjectURL(url);
Binary file
Binary file