ken_nogi/NodeJS/ExcelToPDF/sendPDFtoMerge.js
Kenichiro NOGI 51a95c5e21 NodeJS
2025-07-10 10:12:37 +09:00

61 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch'); // fetchを使用するためにnode-fetchをインポート
// 送信先サーバーのURLとAPIキー
const SERVER_URL = 'https://neo999.next-hd.net:30309/pdfMerge'; // サーバーのURLに合わせて変更
const API_KEY = 'pgqhLFWbDFu4Byz#4afNYX2F6Fa1&$KPjved$8%sUdTQV52caip#EpIKxYUkdd4S'; // サーバーと同じAPIキー
// ローカルPDFファイルのパスを指定
const pdfFile1 = path.join(__dirname, 'file1.pdf');
const pdfFile2 = path.join(__dirname, 'file2.pdf');
// PDFファイルをサーバーに送信してマージする関数
async function sendPDFsForMerge() {
try {
// PDFファイルを読み込む
const pdfData1 = fs.readFileSync(pdfFile1);
const pdfData2 = fs.readFileSync(pdfFile2);
const fileName = path.basename(pdfFile1); // ファイル名を取得file1.pdfの名前を使用
// Base64エンコード
const base64Pdf1 = pdfData1.toString('base64');
const base64Pdf2 = pdfData2.toString('base64');
// リクエストボディを作成
const pdfDataJson = {
base64_1: base64Pdf1,
base64_2: base64Pdf2,
fileName: fileName // マージ後のファイル名を指定
};
// fetchを使用してサーバーにPOSTリクエストを送信
const response = await fetch(SERVER_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
body: JSON.stringify(pdfDataJson)
});
if (!response.ok) {
throw new Error(`HTTPエラー: ${response.status}`);
}
// レスポンスからPDFデータをバイナリ形式で取得
const pdfBuffer = await response.buffer();
// マージされたPDFをファイルに保存
const mergedPdfPath = path.join(__dirname, 'merged.pdf');
fs.writeFileSync(mergedPdfPath, pdfBuffer);
console.log(`マージされたPDFを保存しました: ${mergedPdfPath}`);
} catch (error) {
console.error('エラーが発生しました:', error.message);
}
}
// 関数を実行
sendPDFsForMerge();