{"id":2876,"date":"2026-06-10T13:58:52","date_gmt":"2026-06-10T05:58:52","guid":{"rendered":"https:\/\/thereisno.top\/?p=2876"},"modified":"2026-06-10T13:58:52","modified_gmt":"2026-06-10T05:58:52","slug":"%e9%92%89%e9%92%89%e8%87%aa%e5%8a%a8%e5%8c%96pdf%e5%8a%a0%e5%af%86%e6%95%b0%e5%ad%97%e8%af%81%e4%b9%a6%e7%ad%be%e5%90%8d","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=2876","title":{"rendered":"\u9489\u9489\u81ea\u52a8\u5316PDF\u52a0\u5bc6\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u9762<a href=\"https:\/\/thereisno.top\/?p=2874\">python \u81ea\u52a8\u5316pdf\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d<\/a>\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u901a\u8fc7python\u5b9e\u73b0PDF\u52a0\u5bc6\u548c\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d\uff0c\u4f46\u662f\u8fd8\u9700\u8981\u4f9d\u8d56\u7535\u8111\uff0c\u5f53\u6211\u4eec\u6b63\u5728\u516c\u56ed\u73a9\u800d\u65f6\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u6709PDF\u6587\u4ef6\u5904\u7406\u9700\u6c42\u600e\u4e48\u529e\uff1f\u96be\u9053\u8981\u80cc\u7740\u7535\u8111\u53bb\u73a9\u800d\uff1f\u6216\u8005\u8dd1\u56de\u5bb6\u5904\u7406\uff1fNo! \u53ef\u4ee5\u901a\u8fc7\u9489\u9489\/\u5fae\u4fe1\/\u98de\u4e66\u7b49\u673a\u5668\u4eba\u5728\u4e91\u7aef\u670d\u52a1\u5668\u5904\u7406\uff0c\u7136\u540e\u4ee5\u6d88\u606f\u7684\u5f62\u5f0f\u56de\u590d\u56de\u6765\u3002\u76ee\u524d\u53d1\u73b0\u5fae\u4fe1\u673a\u5668\u4eba\u4e0d\u652f\u6301\u63a5\u6536\u6587\u4ef6\uff0c\u6216\u8bb8\u975e\u8ba4\u8bc1\u7528\u6237\u6ca1\u6709\u6743\u9650\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u6b65\u9aa4<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53d1\u9001\u6587\u4ef6\u7ed9\u9489\u9489<\/li>\n\n\n\n<li>\u7136\u540e\u63a5\u6536\u6587\u4ef6\u5b58\u50a8\u5230\u4e91\u7aef\u670d\u52a1\u5668<\/li>\n\n\n\n<li>\u7136\u540e\u901a\u8fc7\u5df2\u7ecf\u505a\u597d\u7684python\u4ee3\u7801\u5bf9\u6587\u4ef6\u8fdb\u884cPDF\u52a0\u5bc6\u548c\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d<\/li>\n\n\n\n<li>\u7136\u540e\u5c06\u5904\u7406\u540e\u7684\u6587\u4ef6\u518d\u901a\u8fc7\u673a\u5668\u4eba\u56de\u590d<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u4ee3\u7801<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u96be\u70b91\uff1ajava\u8c03\u7528python<\/h3>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-preformatted\">import java.io.BufferedReader;\nimport java.io.InputStreamReader;\nimport java.lang.reflect.Array;\n\npublic class JavaCallPython {\n    \n   public static void main(String [] args) {\n       try {\n           \/\/ ===================== \u5fc5\u987b\u6539\u6210\u4f60\u81ea\u5df1\u7684\u8def\u5f84 =====================\n           String pythonPath = \"\/Library\/Frameworks\/Python.framework\/Versions\/3.10\/bin\/python3\";  \/\/ macOS\/Homebrew\n           \/\/ String pythonPath = \"python\";               \/\/ Windows\n           String scriptPath = \"\/Users\/Downloads\/signpdf.py\";\n           String param = \"\/Users\/input.pdf\"; \/\/ \u8981\u4f20\u7ed9 Python \u7684\u53c2\u6570, \u6587\u4ef6\u540d\u53ef\u80fd\u6709\u7279\u6b8a\u5b57\u7b26\u7528\u5f15\u53f7\u62ec\u8d77\u6765\n           \/\/ ==============================================================\n           \n           \/\/ \u6784\u5efa\u547d\u4ee4\uff1apython3 \u811a\u672c\u8def\u5f84 \u53c2\u65701 \u53c2\u65702...\n           String[] commandArr = new String[]{\n                   pythonPath,\n                   scriptPath,\n                   \"-f\",\n                   param\n           };\n           \n           System.out.println(Arrays.toString(commandArr));\n\n           \/\/ \u6267\u884c\u811a\u672c\n           Process process = Runtime.getRuntime().exec(commandArr);\n\n           \/\/ \u83b7\u53d6 Python \u8f93\u51fa\u5185\u5bb9\n           BufferedReader reader = new BufferedReader(\n                   new InputStreamReader(process.getInputStream())\n           );\n\n           \/\/ \u8bfb\u53d6\u6bcf\u4e00\u884c print\n           String line;\n           System.out.println(\"=== Python \u8f93\u51fa ===\");\n           while ((line = reader.readLine()) != null) {\n               System.out.println(line);\n           }\n\n           \/\/ \u7b49\u5f85\u811a\u672c\u6267\u884c\u5b8c\u6210\n           int exitCode = process.waitFor();\n           System.out.println(\"==================\");\n           System.out.println(\"Python \u9000\u51fa\u7801\uff1a\" + exitCode + \" (0=\u6210\u529f)\");\n\n           reader.close();\n           process.destroy();\n\n       } catch (Exception e) {\n           e.printStackTrace();\n       }\n   }\n}\n<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>\u63d0\u793a<\/strong>\uff1a \u6211\u4eec\u7684\u53c2\u6570\u683c\u5f0f\u662f&nbsp;<code>python3&nbsp;signpdf.py&nbsp;-f&nbsp;file.pdf<\/code>&nbsp;\u683c\u5f0f\uff0c&nbsp;<code>commandArr<\/code>\u4e2d\u7684\u6bcf\u4e00\u9879\u662f<code>\u4e0d\u80fd\u5305\u542b\u7a7a\u683c<\/code>\uff0c\u4e0d\u80fd\u628a\u4e24\u4e2a\u53c2\u6570<code>-f&nbsp;file.pdf<\/code>&nbsp;\u653e\u5728\u4e00\u4e2a\u6570\u7ec4\u9879\u4e2d\uff0c\u5fc5\u987b\u5355\u72ec\u4e00\u9879\u3002 \u8fd9\u5730\u65b9\u662f\u4e00\u4e2a\u5751\uff0c\u6253\u5370\u51fa\u6765\u7684\u547d\u4ee4\u53c2\u6570\u6ca1\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u6267\u884c\uff0c\u4f46\u662f\u5c31\u662f\u4e0d\u6267\u884c\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u96be\u70b92\uff1a\u56de\u590d\u6587\u4ef6\u4fe1\u606f<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.apache.commons.io.FileUtils;\nimport org.apache.commons.io.IOUtils;\nimport org.apache.http.HttpEntity;\nimport org.apache.http.HttpHeaders;\nimport org.apache.http.client.methods.CloseableHttpResponse;\nimport org.apache.http.client.methods.HttpGet;\nimport org.apache.http.client.methods.HttpPost;\nimport org.apache.http.entity.ContentType;\nimport org.apache.http.entity.StringEntity;\nimport org.apache.http.entity.mime.MultipartEntityBuilder;\nimport org.apache.http.impl.client.CloseableHttpClient;\nimport org.apache.http.impl.client.HttpClients;\nimport org.apache.http.util.EntityUtils;\n\nimport com.alibaba.fastjson.JSONObject;\n\nimport java.io.File;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.nio.charset.StandardCharsets;\n\n\/**\n * \u9489\u9489\u6587\u4ef6\u4e0a\u4f20\/\u4e0b\u8f7d\u5de5\u5177\u7c7b\uff08\u7eafHttpClient + Common IO\uff0c\u65e0SDK\u4f9d\u8d56\uff09\n *\/\npublic class FileUpDownUtil {\n\t\/\/ \u9489\u9489\u673a\u5668\u4eba\u4e13\u7528\u4e0b\u8f7d\u63a5\u53e3\n    private final String DOWNLOAD_API_URL = \"https:\/\/api.dingtalk.com\/v1.0\/robot\/messageFiles\/download\";\n\n\/\/    \/\/ ====================== 1. \u83b7\u53d6\u9489\u9489 access_token ======================\n\/\/    public static String getAccessToken(String appKey, String appSecret) throws Exception {\n\/\/        CloseableHttpClient client = HttpClients.createDefault();\n\/\/        HttpPost post = new HttpPost(\"https:\/\/oapi.dingtalk.com\/gettoken\");\n\/\/\n\/\/        JSONObject param = new JSONObject();\n\/\/        param.put(\"appkey\", appKey);\n\/\/        param.put(\"appsecret\", appSecret);\n\/\/\n\/\/        post.setHeader(HttpHeaders.CONTENT_TYPE, \"application\/json\");\n\/\/        post.setEntity(new org.apache.http.entity.StringEntity(param.toString(), StandardCharsets.UTF_8));\n\/\/\n\/\/        CloseableHttpResponse resp = client.execute(post);\n\/\/        String result = IOUtils.toString(resp.getEntity().getContent(), StandardCharsets.UTF_8);\n\/\/        resp.close();\n\/\/        client.close();\n\/\/\n\/\/        JSONObject json = JSONObject.parseObject(result);\n\/\/        return json.getString(\"access_token\");\n\/\/    }\n\n    \/\/ ====================== 2. \u4e0a\u4f20\u6587\u4ef6\u5230\u9489\u9489\uff08\u83b7\u53d6 media_id\uff09 ======================\n    public String uploadFile(String accessToken, File file) throws Exception {\n        CloseableHttpClient client = HttpClients.createDefault();\n        HttpPost post = new HttpPost(\"https:\/\/oapi.dingtalk.com\/media\/upload?access_token=\" + accessToken);\n\n        HttpEntity entity = MultipartEntityBuilder.create()\n                .addBinaryBody(\"media\", file, ContentType.DEFAULT_BINARY, file.getName())\n                .addTextBody(\"type\", \"file\")\n                .build();\n\n        post.setEntity(entity);\n        CloseableHttpResponse resp = client.execute(post);\n        String result = IOUtils.toString(resp.getEntity().getContent(), StandardCharsets.UTF_8);\n\n        resp.close();\n        client.close();\n        System.out.println(result);\n\n        JSONObject json = JSONObject.parseObject(result);\n        return json.getString(\"media_id\");\n    }\n    \n   \n\n    \/\/ ====================== 3. \u4e0b\u8f7d\u9489\u9489\u6587\u4ef6\uff08media_id \u2192 \u672c\u5730\u6587\u4ef6\uff09 ======================\n    public void downloadFile(String accessToken, String mediaId, File saveFile) throws Exception {\n        CloseableHttpClient client = HttpClients.createDefault();\n        String url = \"https:\/\/oapi.dingtalk.com\/media\/downloadFile?access_token=\" + accessToken + \"&amp;media_id=\" + mediaId;\n        System.out.println(url);\n        HttpGet get = new HttpGet(url);\n        CloseableHttpResponse resp = client.execute(get);\n        InputStream in = resp.getEntity().getContent();\n\n        \/\/ Common IO \u76f4\u63a5\u4fdd\u5b58\n        FileUtils.copyInputStreamToFile(in, saveFile);\n\n        in.close();\n        resp.close();\n        client.close();\n    }\n    \n    \/**\n     * \u8c03\u7528\u9489\u9489\u63a5\u53e3\u83b7\u53d6\u6587\u4ef6\u4e34\u65f6\u4e0b\u8f7d\u94fe\u63a5\n     *\/\n    public String getDownloadUrl(String accessToken, String downloadCode, String robotCode) throws IOException {\n        \/\/ \u4f7f\u7528 try-with-resources \u786e\u4fdd\u8d44\u6e90\u81ea\u52a8\u91ca\u653e\uff0c\u9632\u6b62\u8fde\u63a5\u6cc4\u6f0f\n        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {\n            HttpPost httpPost = new HttpPost(DOWNLOAD_API_URL);\n            \n            \/\/ \u8bbe\u7f6e\u8bf7\u6c42\u5934\n            httpPost.setHeader(\"Content-Type\", \"application\/json\");\n            httpPost.setHeader(\"x-acs-dingtalk-access-token\", accessToken);\n\n            \/\/ \u6784\u9020\u8bf7\u6c42\u4f53 JSON\n            String jsonBody = String.format(\n                    \"{\\\"downloadCode\\\":\\\"%s\\\",\\\"robotCode\\\":\\\"%s\\\"}\", \n                    downloadCode, \n                    robotCode\n            );\n            httpPost.setEntity(new StringEntity(jsonBody, \"UTF-8\"));\n\n            \/\/ \u6267\u884c\u8bf7\u6c42\u5e76\u89e3\u6790\u54cd\u5e94\n            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {\n                String result = EntityUtils.toString(response.getEntity(), \"UTF-8\");\n                System.out.println(\"\u9489\u9489\u63a5\u53e3\u8fd4\u56de\u7ed3\u679c: \" + result);\n                \n                JSONObject json=JSONObject.parseObject(result);\n                \n                return json.getString(\"downloadUrl\"); \n            }\n        }\n    }\n    public void downloadFile(String url, File saveFile) throws Exception {\n        CloseableHttpClient client = HttpClients.createDefault();\n\n        HttpGet get = new HttpGet(url);\n        CloseableHttpResponse resp = client.execute(get);\n        InputStream in = resp.getEntity().getContent();\n\n        \/\/ Common IO \u76f4\u63a5\u4fdd\u5b58\n        FileUtils.copyInputStreamToFile(in, saveFile);\n\n        in.close();\n        resp.close();\n        client.close();\n    }\n\n    \/\/ ====================== 4. \u53d1\u9001\u6587\u4ef6\u6d88\u606f\u7ed9\u7528\u6237 ======================\n    \/**\n     * \u5411\u9489\u9489\u7fa4\u53d1\u9001\u6587\u4ef6\u6d88\u606f\n     *\/\n\n\tpublic  void sendFileMessage(String accessToken, String robotCode, String userId, String mediaId,\n\t\t\tString fileName, String fileType) {\n\t\t\/\/ 1. \u9489\u9489\u5355\u804a\u53d1\u9001\u6d88\u606f\u63a5\u53e3 URL\n\t\tString url = \"https:\/\/api.dingtalk.com\/v1.0\/robot\/oToMessages\/batchSend\";\n\n\t\ttry (CloseableHttpClient httpClient = HttpClients.createDefault()) {\n\t\t\tHttpPost httpPost = new HttpPost(url);\n\n\t\t\t\/\/ 2. \u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08\u6ce8\u610f\uff1a\u65b0\u7248\u63a5\u53e3\u4f7f\u7528 x-acs-dingtalk-access-token\uff09\n\t\t\thttpPost.setHeader(\"x-acs-dingtalk-access-token\", accessToken);\n\t\t\thttpPost.setHeader(\"Content-Type\", \"application\/json\");\n\n\t\t\t\/\/ 3. \u6784\u5efa msgParam JSON \u5b57\u7b26\u4e32\n\t\t\tJSONObject msgParamJson = new JSONObject();\n\t\t\tmsgParamJson.put(\"mediaId\", mediaId);\n\t\t\tmsgParamJson.put(\"fileName\", fileName);\n\t\t\tmsgParamJson.put(\"fileType\", fileType);\n\n\t\t\t\/\/ 4. \u6784\u5efa\u8bf7\u6c42\u4f53 JSON\n\t\t\tJSONObject requestBody = new JSONObject();\n\t\t\trequestBody.put(\"msgParam\", msgParamJson.toJSONString());\n\t\t\trequestBody.put(\"msgKey\", \"sampleFile\"); \/\/ \u53d1\u9001\u6587\u4ef6\u7684\u56fa\u5b9a msgKey\n\t\t\trequestBody.put(\"robotCode\", robotCode);\n\t\t\trequestBody.put(\"userIds\", new String[] { userId }); \/\/ \u63a5\u6536\u8005\u7684 userId \u6570\u7ec4\n\n\t\t\t\/\/ 5. \u8bbe\u7f6e\u8bf7\u6c42\u4f53\n\t\t\tStringEntity entity = new StringEntity(requestBody.toJSONString(), \"UTF-8\");\n\t\t\thttpPost.setEntity(entity);\n\n\t\t\t\/\/ 6. \u6267\u884c\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\n\t\t\ttry (CloseableHttpResponse response = httpClient.execute(httpPost)) {\n\t\t\t\tString result = EntityUtils.toString(response.getEntity(), \"UTF-8\");\n\t\t\t\tSystem.out.println(\"HTTP Status: \" + response.getStatusLine().getStatusCode());\n\t\t\t\tSystem.out.println(\"Response Body: \" + result);\n\t\t\t}\n\n\t\t} catch (Exception e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n    \n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u524d\u9762python \u81ea\u52a8\u5316pdf\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u901a\u8fc7python\u5b9e\u73b0PDF\u52a0\u5bc6\u548c\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d\uff0c\u4f46 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=2876\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u9489\u9489\u81ea\u52a8\u5316PDF\u52a0\u5bc6\u6570\u5b57\u8bc1\u4e66\u7b7e\u540d\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[310,299,309],"class_list":["post-2876","post","type-post","status-publish","format-standard","hentry","category-java","tag-pdf","tag-299","tag-309"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2876"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2876\/revisions"}],"predecessor-version":[{"id":2877,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2876\/revisions\/2877"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}