Как использовать временное пространство в качестве места назначения с помощью PdfStamper из библиотеки iText?

Я просматриваю pdf-файлы веб-пользователей с Domino Server. У меня есть template.pdf и файл шрифта в моем пакете Java для создания этих файлов PDF без СОХРАНЕНИЯ их на сервере. Однако PdfStamper требует, чтобы я использовал OutputStream, которому нужен путь.

Есть ли в Domino Server временное пространство? Есть ли другой способ реализовать это? Есть ли поддельный путь для установки?

В качестве теста сейчас я сохраняю его на своей локальной машине.


package de.vogella.itext.write;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class FillUpPDF {

    // Get the files from jar/package
    InputStream streamTemplate = getClass().getResourceAsStream("Template1.pdf");
    InputStream streamFont = getClass().getResourceAsStream("Ruritania.ttf");

    // Files
    public static final String sTemplate = "Template1.pdf";
    public static final String sResultPDF = "C:/Test/Bob Info.pdf";
    public static final String sResultFont = "Ruritania.ttf";

    // Manipulates a PDF file source with the file destination as result
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {

        // Read the Template
        PdfReader reader = new PdfReader(src);

        // Copy the template and output it to the destination
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));    //Where to fileoutputstream on Domino Server temporarily

        // Gets the fields on the form
        AcroFields form = stamper.getAcroFields();

        // Create and set the font
        BaseFont newFont = BaseFont.createFont(sResultFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        form.addSubstitutionFont(newFont);

        /* Filling up the fields on the form */
        form.setFieldProperty("text_1", "textfont", newFont, null);
        form.setFieldProperty("text_1", "textsize", new Float(9), null);
        form.setField("text_1", "Bob");

        form.setFieldProperty("text_2", "textfont", newFont, null);
        form.setFieldProperty("text_2", "textsize", new Float(9), null);
        form.setField("text_2", "Gates");

        form.setFieldProperty("text_3", "textfont", newFont, null);
        form.setFieldProperty("text_3", "textsize", new Float(9), null);
        form.setField("text_3", "Oct 17, 2013 at 11:00am");

        form.setFieldProperty("text_4", "textfont", newFont, null);
        form.setFieldProperty("text_4", "textsize", new Float(9), null);
        form.setField("text_4", "Cloud and Smarter Infrastructure");

        // Fixed the field
        stamper.setFormFlattening(true);
        stamper.setFreeTextFlattening(true);

        // Close
        stamper.close();
        reader.close();


       // Next Step: Send out the created pdf file to the user.


    }

    // Main method
    public void main(String[] args) throws Exception {
        FillUpPDF certification = new FillUpPDF();
        //certification.manipulatePdf(sTemplate, sResultPDF);
        certification.manipulatePdf(sTemplate, sResultPDF);
    }
}

person The Intern    schedule 21.10.2013    source источник
comment
См. stackoverflow.com/questions/19047154/   -  person Simon O'Doherty    schedule 21.10.2013
comment
OutputStream, которому нужен путь — существует множество реализаций OutputStream, которые не привязаны к пути, например ByteArrayOutputStreams или ServletOutputStreams.   -  person mkl    schedule 21.10.2013


Ответы (1)


Вам не нужен путь к временному файлу. Вы можете использовать поток вывода из ответа и тем самым отправить PDF прямо в браузер.

Пример:

XspHttpServletResponse response = (XspHttpServletResponse) JSFUtil.getFacesContext().getExternalContext().getResponse();
ServletOutputStream out = response.getOutputStream();
PdfStamper stamper = new PdfStamper(reader, out); 
person Per Henrik Lausten    schedule 21.10.2013