You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by daniel ccss <da...@gmail.com> on 2008/03/04 18:05:10 UTC

PDF+JSF question

In Struts I do this for join 2 jasperreport pdfs:

PdfReader reader1 = new PdfReader(bytes);
PdfReader reader2 = new PdfReader(bytes2);

PdfCopyFields copy =
new PdfCopyFields(new FileOutputStream(
context.getRealPath("/reportes/Calificación
de Derechos por Beneficio
Familiar - Resolución Nº " +
beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));

copy.addDocument(reader1);
copy.addDocument(reader2);
copy.close();


How can I do this en JSF??

Someone told me that use PdfCopyFields

Have you tried passing the OutputStream from the response to the
PdfCopyFields instead of a FileOutputStream?

Can anyone give me an example of this?

Thanks!!

Re: PDF+JSF question

Posted by Laurie Harper <la...@holoweb.net>.
That code creates a new PDF file from two existing byte arrays(?) on the 
file system. The same code should work identically under JSF, the only 
thing needing to be changed being context.getRealPath() -- is that the 
bit you're asking for help with?

You should be able to replace that with something like

     ((ServletContext) FacesContext.getCurrentInstance()
         .getExternalContext().getContext()).getRealPath(...)

(assuming you're deploying your application to a Servlet container, not 
a Portlet container). Does that help?

L.

daniel ccss wrote:
> Thanks for the answer, but I think this is not what I need.
> 
> What i need is a way to join 2 or more reports in one pdf file, like is done
> in struts, with JSF:
> 
> *PdfReader reader1 = new PdfReader(bytes);
> PdfReader reader2 = new PdfReader(bytes2);
> 
> PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(
> context.getRealPath("/reports/report1 " + name + ".pdf")));
> 
> copy.addDocument(reader1);
> copy.addDocument(reader2);
> copy.close();*
> 
> Someone has the code to do this but in JSF?? Thanks!!!
> 
> 
> 
> 
> On Wed, Mar 5, 2008 at 8:43 AM, Sertic Mirko, Bedag <Mi...@bedag.ch>
> wrote:
> 
>>  Hi
>>
>>
>>
>> If you want to send a text as a download file to the browser, so something
>> like this. You can also
>>
>> Send a binary file the same way by modifying this method a little bit :
>>
>>
>>
>>     *public* *void* sentFileToBrowser(FacesContext aContext, String
>> aFileName, String aContent, *boolean* aAttachment) {
>>
>>
>>
>>         *try* {
>>
>>             HttpServletResponse theResponse = (HttpServletResponse)
>> aContext.getExternalContext().getResponse();
>>
>>             theResponse.setCharacterEncoding("UTF-8");
>>
>>
>>
>>             MimeTypeGuesser theMimeTypeGuesser = MimeTypeGuesser.*
>> getInstance*();
>>
>>             String theContentType =
>> theMimeTypeGuesser.guessMimeTypeFromFileName(aFileName);
>>
>>
>>
>>             *LOGGER*.logDebug("Content type is " + theContentType);
>>
>>
>>
>>             String theContentDisposition = aAttachment ? "attachment" :
>> "inline";
>>
>>
>>
>>             theResponse.setContentType(theContentType);
>>
>>             theResponse.setHeader("Content-disposition",
>> theContentDisposition + "; filename=\"" + aFileName + "\"");
>>
>>
>>
>>             // Make sure it is running with SSL smooth…..
>>
>>             theResponse.setHeader("Pragma", "private");
>>
>>
>>
>>             PrintWriter theWriter = *new* PrintWriter(*new*OutputStreamWriter(
>> theResponse.getOutputStream(), "UTF-8"));
>>
>>             theWriter.print(aContent);
>>
>>             theWriter.flush();
>>
>>
>>
>>             theWriter.close();
>>
>>
>>
>>             aContext.responseComplete();
>>
>>
>>
>>         } *catch* (Exception e) {
>>
>>             *throw* *new* FacesException("Error sending file", e);
>>
>>         }
>>
>>     }
>>
>>
>>
>> Regards
>>
>> Mirko
>>
>>
>>
>> *Von:* daniel ccss [mailto:danielccss2@gmail.com]
>> *Gesendet:* Dienstag, 4. März 2008 20:20
>> *An:* users@myfaces.apache.org
>> *Betreff:* Re: PDF+JSF question
>>
>>
>>
>> Anybody??
>>
>> On Tue, Mar 4, 2008 at 11:05 AM, daniel ccss <da...@gmail.com>
>> wrote:
>>
>> In Struts I do this for join 2 jasperreport pdfs:
>>
>> PdfReader reader1 = new PdfReader(bytes);
>> PdfReader reader2 = new PdfReader(bytes2);
>>
>> PdfCopyFields copy =
>> new PdfCopyFields(new FileOutputStream( context.getRealPath("/reportes/Calificación
>> de Derechos por Beneficio
>> Familiar - Resolución Nº " +
>> beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));
>>
>> copy.addDocument(reader1);
>> copy.addDocument(reader2);
>> copy.close();
>>
>>
>> How can I do this en JSF??
>>
>> Someone told me that use PdfCopyFields
>>
>> Have you tried passing the OutputStream from the response to the
>> PdfCopyFields instead of a FileOutputStream?
>>
>> Can anyone give me an example of this?
>>
>> Thanks!!
>>
>>
>>
> 



AW: PDF+JSF question

Posted by "Sertic Mirko, Bedag" <Mi...@bedag.ch>.
Use a ByteArrayOutputStream to write the result to a byte array, and then use the following method in your backing bean to send it to the browser, and that's it:

(Adapted from the MyFaces Wiki http://wiki.apache.org/myfaces/Sending_Files )

 

    public void sentFileToBrowser(FacesContext aContext, String aFileName, byte[] aContent, boolean aAttachment) {

 

        try {

            HttpServletResponse theResponse = (HttpServletResponse) aContext.getExternalContext().getResponse();

 

            MimeTypeGuesser theMimeTypeGuesser = MimeTypeGuesser.getInstance();

            String theContentType = theMimeTypeGuesser.guessMimeTypeFromFileName(aFileName);

 

            LOGGER.logDebug("Content type is " + theContentType);

 

            String theContentDisposition = aAttachment ? "attachment" : "inline";

            int contentLength = aContent.length;

 

            LOGGER.logDebug("Content length is " + contentLength);

 

            theResponse.setContentType(theContentType);

            theResponse.setContentLength(contentLength);

            theResponse.setHeader("Content-disposition", theContentDisposition + "; filename=\"" + aFileName + "\"");

 

            // SSL!!! IE is HELL

            theResponse.setHeader("Pragma", "private");

 

            OutputStream theOutput = theResponse.getOutputStream();

            theOutput.write(aContent);

            theOutput.close();

 

            aContext.responseComplete();

 

        } catch (Exception e) {

            throw new FacesException("Error sending file", e);

        }

    }

 

 

 

Regards

Mirko

 

Von: daniel ccss [mailto:danielccss2@gmail.com] 
Gesendet: Mittwoch, 5. März 2008 16:02
An: MyFaces Discussion
Betreff: Re: PDF+JSF question

 

Thanks for the answer, but I think this is not what I need.

 

What i need is a way to join 2 or more reports in one pdf file, like is done in struts, with JSF:

 

PdfReader reader1 = new PdfReader(bytes);
PdfReader reader2 = new PdfReader(bytes2);
 
PdfCopyFields copy = new PdfCopyFields(new FileOutputStream( context.getRealPath("/reports/report1 " + name + ".pdf")));
 
copy.addDocument(reader1);
copy.addDocument(reader2);
copy.close();

 

Someone has the code to do this but in JSF?? Thanks!!!

 



 

On Wed, Mar 5, 2008 at 8:43 AM, Sertic Mirko, Bedag <Mi...@bedag.ch> wrote:

Hi

 

If you want to send a text as a download file to the browser, so something like this. You can also

Send a binary file the same way by modifying this method a little bit :

 

    public void sentFileToBrowser(FacesContext aContext, String aFileName, String aContent, boolean aAttachment) {

 

        try {

            HttpServletResponse theResponse = (HttpServletResponse) aContext.getExternalContext().getResponse();

            theResponse.setCharacterEncoding("UTF-8");

 

            MimeTypeGuesser theMimeTypeGuesser = MimeTypeGuesser.getInstance();

            String theContentType = theMimeTypeGuesser.guessMimeTypeFromFileName(aFileName);

 

            LOGGER.logDebug("Content type is " + theContentType);

 

            String theContentDisposition = aAttachment ? "attachment" : "inline";

 

            theResponse.setContentType(theContentType);

            theResponse.setHeader("Content-disposition", theContentDisposition + "; filename=\"" + aFileName + "\"");

 

            // Make sure it is running with SSL smooth.....

            theResponse.setHeader("Pragma", "private");

 

            PrintWriter theWriter = new PrintWriter(new OutputStreamWriter(theResponse.getOutputStream(), "UTF-8"));

            theWriter.print(aContent);

            theWriter.flush();

 

            theWriter.close();

 

            aContext.responseComplete();

 

        } catch (Exception e) {

            throw new FacesException("Error sending file", e);

        }

    }

 

Regards

Mirko

 

Von: daniel ccss [mailto:danielccss2@gmail.com] 
Gesendet: Dienstag, 4. März 2008 20:20
An: users@myfaces.apache.org
Betreff: Re: PDF+JSF question

 

Anybody??

On Tue, Mar 4, 2008 at 11:05 AM, daniel ccss <da...@gmail.com> wrote:

In Struts I do this for join 2 jasperreport pdfs:

PdfReader reader1 = new PdfReader(bytes);
PdfReader reader2 = new PdfReader(bytes2);
 
PdfCopyFields copy = 
new PdfCopyFields(new FileOutputStream( context.getRealPath("/reportes/Calificación de Derechos por Beneficio 
Familiar - Resolución Nº " + 
beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));
 
copy.addDocument(reader1);
copy.addDocument(reader2);
copy.close();


How can I do this en JSF??

Someone told me that use PdfCopyFields 

Have you tried passing the OutputStream from the response to the
PdfCopyFields instead of a FileOutputStream?

Can anyone give me an example of this?

Thanks!!

 

 


Re: PDF+JSF question

Posted by daniel ccss <da...@gmail.com>.
Thanks for the answer, but I think this is not what I need.

What i need is a way to join 2 or more reports in one pdf file, like is done
in struts, with JSF:

*PdfReader reader1 = new PdfReader(bytes);
PdfReader reader2 = new PdfReader(bytes2);

PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(
context.getRealPath("/reports/report1 " + name + ".pdf")));

copy.addDocument(reader1);
copy.addDocument(reader2);
copy.close();*

Someone has the code to do this but in JSF?? Thanks!!!




On Wed, Mar 5, 2008 at 8:43 AM, Sertic Mirko, Bedag <Mi...@bedag.ch>
wrote:

>  Hi
>
>
>
> If you want to send a text as a download file to the browser, so something
> like this. You can also
>
> Send a binary file the same way by modifying this method a little bit :
>
>
>
>     *public* *void* sentFileToBrowser(FacesContext aContext, String
> aFileName, String aContent, *boolean* aAttachment) {
>
>
>
>         *try* {
>
>             HttpServletResponse theResponse = (HttpServletResponse)
> aContext.getExternalContext().getResponse();
>
>             theResponse.setCharacterEncoding("UTF-8");
>
>
>
>             MimeTypeGuesser theMimeTypeGuesser = MimeTypeGuesser.*
> getInstance*();
>
>             String theContentType =
> theMimeTypeGuesser.guessMimeTypeFromFileName(aFileName);
>
>
>
>             *LOGGER*.logDebug("Content type is " + theContentType);
>
>
>
>             String theContentDisposition = aAttachment ? "attachment" :
> "inline";
>
>
>
>             theResponse.setContentType(theContentType);
>
>             theResponse.setHeader("Content-disposition",
> theContentDisposition + "; filename=\"" + aFileName + "\"");
>
>
>
>             // Make sure it is running with SSL smooth…..
>
>             theResponse.setHeader("Pragma", "private");
>
>
>
>             PrintWriter theWriter = *new* PrintWriter(*new*OutputStreamWriter(
> theResponse.getOutputStream(), "UTF-8"));
>
>             theWriter.print(aContent);
>
>             theWriter.flush();
>
>
>
>             theWriter.close();
>
>
>
>             aContext.responseComplete();
>
>
>
>         } *catch* (Exception e) {
>
>             *throw* *new* FacesException("Error sending file", e);
>
>         }
>
>     }
>
>
>
> Regards
>
> Mirko
>
>
>
> *Von:* daniel ccss [mailto:danielccss2@gmail.com]
> *Gesendet:* Dienstag, 4. März 2008 20:20
> *An:* users@myfaces.apache.org
> *Betreff:* Re: PDF+JSF question
>
>
>
> Anybody??
>
> On Tue, Mar 4, 2008 at 11:05 AM, daniel ccss <da...@gmail.com>
> wrote:
>
> In Struts I do this for join 2 jasperreport pdfs:
>
> PdfReader reader1 = new PdfReader(bytes);
> PdfReader reader2 = new PdfReader(bytes2);
>
> PdfCopyFields copy =
> new PdfCopyFields(new FileOutputStream( context.getRealPath("/reportes/Calificación
> de Derechos por Beneficio
> Familiar - Resolución Nº " +
> beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));
>
> copy.addDocument(reader1);
> copy.addDocument(reader2);
> copy.close();
>
>
> How can I do this en JSF??
>
> Someone told me that use PdfCopyFields
>
> Have you tried passing the OutputStream from the response to the
> PdfCopyFields instead of a FileOutputStream?
>
> Can anyone give me an example of this?
>
> Thanks!!
>
>
>

AW: PDF+JSF question

Posted by "Sertic Mirko, Bedag" <Mi...@bedag.ch>.
Hi

 

If you want to send a text as a download file to the browser, so something like this. You can also

Send a binary file the same way by modifying this method a little bit :

 

    public void sentFileToBrowser(FacesContext aContext, String aFileName, String aContent, boolean aAttachment) {

 

        try {

            HttpServletResponse theResponse = (HttpServletResponse) aContext.getExternalContext().getResponse();

            theResponse.setCharacterEncoding("UTF-8");

 

            MimeTypeGuesser theMimeTypeGuesser = MimeTypeGuesser.getInstance();

            String theContentType = theMimeTypeGuesser.guessMimeTypeFromFileName(aFileName);

 

            LOGGER.logDebug("Content type is " + theContentType);

 

            String theContentDisposition = aAttachment ? "attachment" : "inline";

 

            theResponse.setContentType(theContentType);

            theResponse.setHeader("Content-disposition", theContentDisposition + "; filename=\"" + aFileName + "\"");

 

            // Make sure it is running with SSL smooth.....

            theResponse.setHeader("Pragma", "private");

 

            PrintWriter theWriter = new PrintWriter(new OutputStreamWriter(theResponse.getOutputStream(), "UTF-8"));

            theWriter.print(aContent);

            theWriter.flush();

 

            theWriter.close();

 

            aContext.responseComplete();

 

        } catch (Exception e) {

            throw new FacesException("Error sending file", e);

        }

    }

 

Regards

Mirko

 

Von: daniel ccss [mailto:danielccss2@gmail.com] 
Gesendet: Dienstag, 4. März 2008 20:20
An: users@myfaces.apache.org
Betreff: Re: PDF+JSF question

 

Anybody??

On Tue, Mar 4, 2008 at 11:05 AM, daniel ccss <da...@gmail.com> wrote:

In Struts I do this for join 2 jasperreport pdfs:

PdfReader reader1 = new PdfReader(bytes);
PdfReader reader2 = new PdfReader(bytes2);
 
PdfCopyFields copy = 
new PdfCopyFields(new FileOutputStream( context.getRealPath("/reportes/Calificación de Derechos por Beneficio 
Familiar - Resolución Nº " + 
beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));
 
copy.addDocument(reader1);
copy.addDocument(reader2);
copy.close();


How can I do this en JSF??

Someone told me that use PdfCopyFields 

Have you tried passing the OutputStream from the response to the
PdfCopyFields instead of a FileOutputStream?

Can anyone give me an example of this?

Thanks!!

 


Re: PDF+JSF question

Posted by daniel ccss <da...@gmail.com>.
Sorry but this user group was made to ask, so I ASK, in fact many users
maybe don´t saw the email yesterday and when I ask again if anybody knows?
Mirko.Sertic@bedag.ch answer me, so I think YOU should stop sending  mails
with not answers and saying that do a google search jajajaja if you don´t
know the answer just don´t reply.

Thanks

On Wed, Mar 5, 2008 at 9:23 AM, Matthias Wessendorf <ma...@apache.org>
wrote:

> Hello,
>
> On Wed, Mar 5, 2008 at 2:56 PM, daniel ccss <da...@gmail.com> wrote:
> > Anybody??
>
> sending emails every 10 hours doesn't encourage folks to answer.
> The topic PDF and JSF was already discussed here (and in other forums).
>
> Doing a google search (or on the myfaces archives) mostly helps.
>
> -M
>
> >
>
>
>
> --
> Matthias Wessendorf
>
> further stuff:
> blog: http://matthiaswessendorf.wordpress.com/
> sessions: http://www.slideshare.net/mwessendorf
> mail: matzew-at-apache-dot-org
>

Re: PDF+JSF question

Posted by Matthias Wessendorf <ma...@apache.org>.
Hello,

On Wed, Mar 5, 2008 at 2:56 PM, daniel ccss <da...@gmail.com> wrote:
> Anybody??

sending emails every 10 hours doesn't encourage folks to answer.
The topic PDF and JSF was already discussed here (and in other forums).

Doing a google search (or on the myfaces archives) mostly helps.

-M

>



-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org

PDF+JSF question

Posted by daniel ccss <da...@gmail.com>.
Anybody??

Re: PDF+JSF question

Posted by daniel ccss <da...@gmail.com>.
Anybody??

On Tue, Mar 4, 2008 at 11:05 AM, daniel ccss <da...@gmail.com> wrote:

> In Struts I do this for join 2 jasperreport pdfs:
>
> PdfReader reader1 = new PdfReader(bytes);
> PdfReader reader2 = new PdfReader(bytes2);
>
> PdfCopyFields copy =
> new PdfCopyFields(new FileOutputStream( context.getRealPath("/reportes/Calificación
> de Derechos por Beneficio
> Familiar - Resolución Nº " +
> beneficioForm.getNumeroResolucionBeneficio() + ".pdf")));
>
> copy.addDocument(reader1);
> copy.addDocument(reader2);
> copy.close();
>
>
> How can I do this en JSF??
>
> Someone told me that use PdfCopyFields
>
> Have you tried passing the OutputStream from the response to the
> PdfCopyFields instead of a FileOutputStream?
>
> Can anyone give me an example of this?
>
> Thanks!!
>