Bonjour à tous,

Pour avoir pas mal cherché il n'est pas facile de trouver un code en Flex 3 (complet et qui fonctionne) sur le net alors j'en profite pour mettre le mien au complet (* il faudra joindre au projet la librairie AlivePDF disponible ici).

Voici le code complet qui permet de créer un pdf depuis une application Flex en s'aidant de quelques lignes de php :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">	
 
	<mx:Script>
		<![CDATA[
 
			import org.alivepdf.colors.RGBColor;
			import org.alivepdf.data.Grid;
			import org.alivepdf.data.GridColumn;
			import org.alivepdf.drawing.Joint;
			import org.alivepdf.fonts.*;
			import org.alivepdf.layout.*;
			import org.alivepdf.pdf.PDF;
			import org.alivepdf.saving.Download;
			import org.alivepdf.saving.Method;
 
			public static const CELL_COLOR:RGBColor = new RGBColor(0);
			public static const BORDER_COLOR:RGBColor = new RGBColor(0x0);
			public static const HEADER_COLOR:RGBColor = new RGBColor(0x666666);
			public static const BACKGROUND_COLOR:RGBColor = new RGBColor(0xCCCCCC);
 
			[Bindable]
			private var provider:Array = [
							{Prénom:'Anthony', Nom:'test1'},
							{Prénom:'David', Nom:'test2'},
							{Prénom:'Pierre', Nom:'test3'},
							{Prénom:'Sébastien', Nom:'test4'}
						];
 
			public function generatePDF():void
			{
				var pdf:PDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.A4);
				var grid:Grid;
 
				grid = new Grid(provider, 0, 0, HEADER_COLOR, BACKGROUND_COLOR, CELL_COLOR, false, BORDER_COLOR, 1, Joint.MITER);
				grid.columns = [new GridColumn("Prénom", "Prénom", 50), new GridColumn("Nom", "Nom", 50)];
 
				//pdf.setDisplayMode(Display.DEFAULT, Layout.SINGLE_PAGE);
 
				pdf.addPage();
				pdf.textStyle(new RGBColor(0), 1);
 
				//DEFINITION D'UNE POLICE POUR LE TITRE
				pdf.setFont(FontFamily.ARIAL , Style.BOLD, 20);
				pdf.addText('Ceci est un titre.', 10, 20);
 
				//REDEFINIR POLICE POUR LA GRID
				pdf.setFont(FontFamily.ARIAL, Style.NORMAL, 10);
				pdf.addGrid(grid, 0, 20);
 
				pdf.save(Method.REMOTE, "http://localhost/create.php", Download.INLINE, "test.pdf");
			}
		]]>
	</mx:Script>
 
	<mx:Button label="Générer PDF" click="generatePDF()"/>
	<mx:DataGrid dataProvider="{provider}"/>
</mx:Application>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
$method = $_GET['method'];
$name = $_GET['name'];
 
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] ))
{       
        // get bytearray
        $pdf = $GLOBALS["HTTP_RAW_POST_DATA"];
        
        // add headers for download dialog-box
        header('Content-Type: application/pdf');
        header('Content-Length: '.strlen($pdf));
        header('Content-disposition:'.$method.'; filename="'.$name.'"');
        echo $pdf;
        
}  else echo 'An error occured.';
 
?>
J'ai quand même une question...
C'est quand même malheureux de devoir passer pas un serveur php juste pour retourner une variable postée (HTTP_RAW_POST_DATA) décorée de trois header non ??

N'y aurait il pas moyen de s'en passer ?
Ou bien il y a un truc qui m'échappe ou bien le code php ne fait bel et bien rien d'autre que de retourner la variable HTTP_RAW_POST_DATA décorée de trois header ??

Merci pour vos avis !