用Java编辑现有的pdf

时间:2014-08-21 16:50:10

标签: java google-app-engine pdf itext pdfbox

我正在测试Java lib以编辑现有的pdf,但问题是无法加载我现有的pdf。 我有与iText和pdfbox相同的结果,我可以加载数据在这里看起来的文件(pdf称重ko),但创建的pdf是空的(没有任何显示)。

我在app引擎服务器上执行此操作,使用两个lib我可以创建pdf并使用servlet或webservice在浏览器中显示它。

我完全迷失了,尝试了大量的代码,但总是一样的结果!

iText with importedPage:

    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter docWriter = PdfWriter.getInstance(document, baos);
    document.open();

    // Load existing PDF
    PdfReader reader = new PdfReader("WEB-INF/pdf.pdf");
    document.newPage();
    PdfImportedPage page = docWriter.getImportedPage(reader, 1);
    PdfPTable table = new PdfPTable(2);
    table.addCell(Image.getInstance(page));
    document.add(table);

    document.close();
    docWriter.close();

pdfbox:

     PDDocument document = new PDDocument();
     PDDocument sourceDocument = PDDocument.load("WEB-INF/pdf.pdf");
     PDPage templatePdfPage = (PDPage)sourceDocument.getDocumentCatalog().getAllPages().get(0);
     document.addPage(templatePdfPage);
     document.save(output);

2 个答案:

答案 0 :(得分:0)

首先获取路径使用ServletContext Servlet并使用PDFBOx读取pdf文件并将pdf文件保存在/WEB-INF/savedpdffiles/文件夹中。

注意:在savedpdffiles文件夹下创建文件夹WEB-INF

请参阅The JRE Class White List - A Java App Engine application's access to the classes in the Java standard library (the Java Runtime Environment, or JRE) is limited to the following classes:

在Google AppEngine中阅读并保存PDF文件。

<强>代码:

    PrintWriter printWriter = response.getWriter();
    try {
        ServletContext context = request.getSession().getServletContext();
        String pdffiles = context.getRealPath("/WEB-INF/");

        File readPath = new File(pdffiles);
        if (readPath.exists()) {
            String pdfFile = "04-Request-Headers.pdf"; // read this file to save in savedpdffiles folder
            File savedPath = new File(readPath.getAbsolutePath() +"/savedpdffiles/"); // create savedpdffiles folder under WEB-INF folder

            File readFullPath = new File(readPath.getAbsolutePath() + File.separatorChar + pdfFile);
            if (readFullPath.isFile()) {
                if(!savedPath.exists()) {
                    savedPath.createNewFile();// create new pdf file if not exits
                    printWriter.println( savedPath.getName() +" File created in -> "+ savedPath.getAbsolutePath());
                }

                PDDocument document = new PDDocument();
                PDDocument sourceDocument = PDDocument.load(readFullPath.getAbsolutePath()); // read the pdf file by PDDocument
                PDPage templatePdfPage = (PDPage) sourceDocument.getDocumentCatalog().getAllPages().get(0); // only first page is read out of 13 pages and save the first page.
                document.addPage(templatePdfPage);
                document.save(savedPath + "/" + pdfFile);
                document.close();
                sourceDocument.close();
                printWriter.print(pdfFile + " File saved to this location-> "+ savedPath.getAbsolutePath() + File.separatorChar + pdfFile);
            } else {
                printWriter.println(readFullPath.getName() + " File not exits in -> "+ readFullPath.getAbsolutePath());
            }
        } else {
            printWriter.println("Path not exists -> "+ readPath.getAbsolutePath());
        }
    } catch (Exception e) {
        printWriter.print("Type of Error occured while saving the PDF file -> "+ e.getMessage());
        e.printStackTrace();
    }

您将收到以下错误

Caused by:
          java.lang.NoClassDefFoundError: java.awt.Color is a restricted class. Please see the Google  App Engine developer's guide for more details.
    Powered by Jetty://

答案 1 :(得分:0)

我发现问题,我正在使用maven来构建我的应用程序。 Maven通过编码文件来破坏我的pdf(我认为是UTF-8)。 我找到了这个,因为我得到了.p12文件,当我读什么时,我的lib说它是腐败文件。

现在的问题是试图避免这种情况,但是不起作用。 (目前我在构建后替换文件)

我尝试在我的pom.xml中添加它:

<resources>
        <resource>
            <directory>src/main/webapp/WEB-INF</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.pdf</exclude>
            </excludes>
        </resource>
    </resources>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>

在我的pom.xml中有些东西,我不知道它做了什么:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <webResources>
                    in order to interpolate version from pom into appengine-web.xml
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>