- ڈویلپر ہوم
- ایڈیٹرز ایمبیڈ کریں
- دستاویز بلڈر
دستاویز بلڈر
دستاویز بلڈر
POST /api/v1/embed/builder کسی اسکرپٹ یا JSON تفصیل سے Word، Excel، PowerPoint اور PDF فائلیں بناتا ہے، یا آپ کی بھیجی ہوئی فائلوں میں تبدیلی کرتا ہے۔ اسکرپٹ JavaScript میں ایک آبجیکٹ ماڈل کے خلاف ہے جس کا نام ONLYOFFICE Document Builder کے ماڈل پر رکھا گیا ہے (builder.CreateFile، Api.GetDocument، Api.CreateParagraph، oWorksheet.GetRange…)، لہٰذا ONLYOFFICE کے لیے لکھا گیا builder اسکرپٹ یہاں ان خصوصیات کے اندر چلتا ہے جو دونوں میں موجود ہیں۔ ہر فائل MR Office کے اپنے ایڈیٹرز اور رائٹرز بناتے ہیں، گویا کسی شخص نے اسے ایڈیٹر میں بنا کر محفوظ کیا ہو۔
builder.CreateFile("docx");
const oDocument = Api.GetDocument();
const oTitle = oDocument.GetElement(0);
oTitle.SetStyle(oDocument.GetStyle("Heading 1"));
oTitle.AddText("Invoice " + Argument.number);
const oTable = Api.CreateTable(3, Argument.lines.length + 1);
["Item", "Quantity", "Amount"].forEach((label, column) =>
oTable.GetCell(0, column).GetContent().GetElement(0).AddText(label));
Argument.lines.forEach((line, row) =>
[line.item, String(line.quantity), line.amount].forEach((value, column) =>
oTable.GetCell(row + 1, column).GetContent().GetElement(0).AddText(value)));
oDocument.Push(oTable);
const oTotal = Api.CreateParagraph();
oTotal.SetJc("right");
oTotal.AddText("Total due: " + Argument.total).SetBold(true);
oDocument.Push(oTotal);
builder.SaveFile("docx", "invoice-" + Argument.number + ".docx");
builder.SaveFile("pdf", "invoice-" + Argument.number + ".pdf");
builder.CloseFile();curl https://mroffice.ai/api/v1/embed/builder \
-H "Authorization: Bearer $MROFFICE_SECRET_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n --rawfile script invoice.js '{
script: $script,
argument: { number: "1042", total: "EUR 462.32",
lines: [{ item: "Fountain pen", quantity: 12, amount: "EUR 222.00" }] }
}')"- script (JavaScript) کو argument کے ساتھ بھیجیں (کوئی بھی JSON: اسکرپٹ اسے Argument کے طور پر پڑھتا ہے)، یا document (Builder JSON) کو output_format اور name کے ساتھ۔ script اور document میں سے بالکل ایک۔
- builder.CreateFile("docx" | "xlsx" | "pptx" | "pdf") starts a new file; builder.OpenFile(name) opens one of the request's inputs (up to five: docx, xlsx, pptx, pdf, each by url, upload_id or data_base64). Each builder.SaveFile(format, name) is one output file, up to ten: a document as docx, docxf (with its form fields), pdf (a fillable PDF form when it has form fields; oform too), pdfa, odt, rtf, doc, html, txt, md, epub or fb2; a workbook as xlsx, pdf, pdfa, ods, xls or csv; a deck as pptx, pdf, pdfa, odp or ppt; and every kind as page pictures (png, jpg, bmp, gif, tiff, webp: the first page, or every page as a ZIP with SaveFile(format, name, { first: false })).
- ایک آؤٹ پٹ جاب کی فائل ہے؛ کئی ہوں تو ZIP کے طور پر آتے ہیں، اور ہر ایک الگ سے بھی ڈاؤن لوڈ ہو سکتا ہے (job.files[n].download_url)۔
- An opened file keeps everything the script does not change: its words are changed with SearchAndReplace, new content is added at its end, a workbook's cells are set, slides are added, a PDF's form fields are filled. A document's paragraphs (oDocument.GetElement(n)) are changed in place: AddText, RemoveAllElements, SetJc, SetBold and the other run formats, Delete, InsertParagraph before or after; everything else of the paragraph stays. Form fields (Api.CreateTextForm, CreateCheckBoxForm, CreateComboBoxForm, CreateDateForm) are Word content controls; an opened document's are read and filled with GetAllForms / SetFormsData. Slides already in an opened deck are read-only.
- جاب کنورژن کی جاب جیسی ہے: async، Idempotency-Key، webhook، polling، download اور retention اسی طرح کام کرتے ہیں، اور ایک build مہینے کے کنورژنز کی ایک اکائی ہے، چاہے وہ کتنی ہی فائلیں محفوظ کرے۔ ناکام اسکرپٹ embed_builder_script_error کے ساتھ اپنا پیغام اور اپنی console.log سطریں واپس کرتا ہے۔
{
"document": {
"type": "docx",
"page": { "size": "a4" },
"footer": "Northwind Traders Ltd",
"content": [
{ "type": "heading", "level": 1, "text": "Quarterly report" },
{ "type": "paragraph", "runs": [{ "text": "Q3 2026. ", "bold": true }, { "text": "Revenue grew in every region." }] },
{ "type": "chart", "chart_type": "column", "title": "Revenue by region",
"categories": ["Americas", "EMEA", "APAC"],
"series": [{ "name": "Q2", "values": [412, 388, 251] }, { "name": "Q3", "values": [431, 446, 297] }] },
{ "type": "table", "headers": ["Region", "Q2", "Q3"], "rows": [["EMEA", "388", "446"], ["APAC", "251", "297"]] },
{ "type": "list", "items": ["EMEA grew 15%", "APAC kept its pace"] }
]
},
"output_format": "pdf",
"name": "report-q3"
}{
"inputs": [{ "url": "https://files.example.com/signed/form.pdf", "name": "form.pdf" }],
"document": {
"type": "pdf",
"open": "form.pdf",
"edits": [{ "action": "fill_form", "fields": { "name": "Ada Lovelace", "plan": "Pro", "agree": true } }]
}
}import { readFile, writeFile } from "node:fs/promises";
import { createClient } from "mroffice-embed/server";
const mro = createClient({ secretKey: process.env.MROFFICE_SECRET_KEY });
const job = await mro.build({
script: await readFile("deck.js", "utf8"),
argument: { title: "Product update", sections: [{ title: "What shipped", points: ["Builder API"] }] },
});
await writeFile(job.result.filename, Buffer.from(await mro.download(job)));
const book = await mro.build({
document: {
type: "xlsx",
sheets: [{ name: "Sales", rows: [["Month", "Sales"], ["Jul", 1200], ["Aug", 1340]],
cells: { B4: { formula: "=SUM(B2:B3)", style: { bold: true } } },
charts: [{ type: "column", range: "A1:B3", title: "Sales" }] }],
},
});
const filled = await mro.build({
script: 'builder.OpenFile("form.pdf"); Api.GetDocument().SetFormsData([{ key: "name", value: Argument.name }]); builder.SaveFile("pdf", "filled.pdf");',
argument: { name: "Ada Lovelace" },
inputs: [{ data: await readFile("form.pdf"), name: "form.pdf" }],
});import base64, os, pathlib, requests
api = "https://mroffice.ai/api/v1/embed"
auth = {"Authorization": f"Bearer {os.environ['MROFFICE_SECRET_KEY']}"}
job = requests.post(f"{api}/builder", headers=auth, json={
"script": pathlib.Path("report.js").read_text(),
"argument": {"title": "Quarterly revenue report", "regions": ["Americas", "EMEA", "APAC"]},
}).json()
if job["status"] == "failed":
raise SystemExit(job["error"]["message"])
pathlib.Path(job["result"]["filename"]).write_bytes(requests.get(job["download_url"]).content)export MROFFICE_SECRET_KEY=mro_sk_...
npx mroffice-embed build invoice.js --argument invoice.json --out ./out
npx mroffice-embed build form-fill.js --argument fields.json --input form.pdf --out ./out
npx mroffice-embed build report.json --format pdf --out ./outآبجیکٹ ماڈل
سائز ONLYOFFICE کی اکائیوں میں ہیں: ڈرائنگز EMU میں (ایک انچ میں 914,400)، صفحے کے سائز twips میں، run کا فونٹ سائز نصف پوائنٹس میں۔ نئی دستاویز ایک خالی پیراگراف سے اور نیا ڈیک ایک سلائیڈ سے شروع ہوتا ہے، جیسے ONLYOFFICE میں۔ سلائیڈز MR Office کے اپنے حصے بھی لیتی ہیں (SetTitle، SetBullets، SetNotes)، جو ڈیک کے تھیم کے مطابق ترتیب دیے جاتے ہیں۔ جو طریقہ MR Office میں نہیں ہے وہ TypeError پھینکتا ہے جس میں اس کا نام ہوتا ہے۔
| آبجیکٹ | طریقے |
|---|---|
builder | .CreateFile · .OpenFile · .SaveFile · .CloseFile · .SetTheme |
Api | .GetDocument · .CreateParagraph · .CreateRun · .CreateTable · .CreateChart · .CreateImage · .CreateShape · .CreateRGBColor · .CreateColorFromRGB · .CreateSolidFill · .CreateNoFill · .CreateStroke · .GetActiveSheet · .GetSheet · .GetSheets · .AddSheet · .GetRange · .GetPresentation · .CreateSlide |
ApiDocument | .GetElement · .GetElementsCount · .Push · .AddElement · .RemoveAllElements · .GetText · .SearchAndReplace · .GetStyle · .CreateNumbering · .GetFinalSection · .GetAllForms · .GetFormsData · .SetFormsData |
ApiParagraph | .AddText · .AddElement · .AddLineBreak · .AddPageBreak · .AddDrawing · .SetJc · .SetStyle · .SetNumbering · .SetBold · .SetItalic · .SetFontSize · .SetFontFamily · .SetColor · .GetText |
ApiRun | .AddText · .AddLineBreak · .SetBold · .SetItalic · .SetUnderline · .SetStrikeout · .SetFontSize · .SetFontFamily · .SetColor · .SetHighlight |
ApiTable | .GetCell · .GetRow · .GetRowsCount · .AddRow · .SetTableLook · .SetTitle · .SetSize · .SetPosition |
ApiSection | .GetHeader · .GetFooter · .SetPageSize · .SetPageMargins |
ApiForm | .GetFormKey · .GetFormType · .GetText · .GetListValues · .IsChecked · .IsRequired · .SetText · .SetChecked · .SelectListValue |
ApiWorksheet | .GetName · .SetName · .GetRange · .GetRangeByNumber · .GetCells · .GetUsedRange · .SetColumnWidth · .SetRowHeight · .GetFreezePanes · .AddChart |
ApiRange | .SetValue · .SetFormula · .GetValue · .GetText · .Clear · .SetBold · .SetItalic · .SetUnderline · .SetFontSize · .SetFontName · .SetFontColor · .SetFillColor · .SetNumberFormat · .SetWrap · .SetAlignHorizontal · .SetAlignVertical · .SetBorders · .Merge · .UnMerge · .GetAddress |
ApiPresentation | .GetSlideByIndex · .GetSlidesCount · .AddSlide · .RemoveSlides · .SetSizes · .SearchAndReplace |
ApiSlide | .AddObject · .RemoveAllObjects · .SetBackground · .GetNotesPage · .SetLayout · .SetTitle · .SetSubtitle · .SetBullets · .SetNotes |
ApiShape · ApiChart · ApiImage | .GetDocContent · .SetSize · .SetPosition · .SetTitle · .SetLegendPos · .SetDescription |
/// <reference types="mroffice-embed/builder" />
builder.CreateFile("xlsx");
const oSheet = Api.GetActiveSheet();
oSheet.GetRange("A1:B1").SetValue([["Month", "Sales"]]);
oSheet.GetRange("A2:B3").SetValue([["Jul", 1200], ["Aug", 1340]]);
oSheet.GetRange("B4").SetValue("=SUM(B2:B3)");
oSheet.GetRange("B2:B4").SetNumberFormat("#,##0.00");
oSheet.AddChart("'Sheet1'!$A$1:$B$3", false, "bar", 2);
builder.SaveFile("xlsx", "sales.xlsx");
builder.CloseFile();بلڈر JSON
JavaScript کے بغیر وہی فائلیں: نئی فائل کی قسم اس کے مواد، شیٹس یا سلائیڈز کے ساتھ، یا open سے نامزد کردہ ان پٹ اس کی ترمیمات کے ساتھ۔ اسکرپٹ کا SaveFile سرور کو بالکل یہی تفصیل دیتا ہے، لہٰذا دونوں طریقے ایک ہی فائل لکھتے ہیں۔ غلطی کا جواب فوراً embed_builder_invalid اور details.path (document.content[3].type) کے ساتھ دیا جاتا ہے، کچھ بھی گنے جانے سے پہلے۔
{ "type": "docx" | "pdf", "content": [
{ "type": "paragraph" | "heading" | "quote", "text": "…" | "runs": [{ "text", "bold", "italic", "underline", "strike", "size", "font", "color", "highlight" }], "level": 1-6, "align" },
{ "type": "list", "ordered": false, "items": ["…"] },
{ "type": "table", "headers": ["…"], "rows": [["…"]], "title": "…" },
{ "type": "chart", "chart_type": "column" | "bar" | "line" | "area" | "pie" | "radar", "title", "categories": [], "series": [{ "name", "values": [] }], "legend" },
{ "type": "image", "src": "https://…" | "data:image/png;base64,…", "width", "alt" },
{ "type": "page_break" } ],
"page": { "size": "a4" | "letter", "margins": "normal" | "narrow" | "wide" }, "header", "footer", "page_numbers": "none" | "page" | "page-of-total", "theme" }
{ "type": "xlsx", "sheets": [{ "name", "rows": [[…]], "cells": { "A1": 1, "B1": { "value", "formula": "=SUM(A1:A9)", "style": {
"bold", "italic", "underline", "color", "background_color", "number_format", "font_family", "font_size", "text_align", "vertical_align", "wrap_text", "border" } } },
"column_widths": { "A": 24 }, "row_heights": { "1": 20 }, "freeze_rows", "freeze_columns", "merges": ["A1:C1"],
"charts": [{ "type": "column" | "bar" | "line" | "pie" | "area" | "scatter", "range": "A1:C5", "anchor": "E2", "title" }] }], "theme" }
{ "type": "pptx", "slides": [{ "layout", "title", "subtitle", "bullets": [], "notes", "background": "#f8fafc",
"objects": [{ "type": "text" | "shape" | "table" | "chart" | "image", "x": 10, "y": 20, "width": 40, "height": 30, … }] }], "theme" }
{ "type": "docx", "open": "contract.docx", "edits": [{ "action": "replace_text", "find", "replace" }, { "action": "append", "content": [] }] }
{ "type": "xlsx", "open": "book.xlsx", "edits": [{ "action": "set_cells", "sheet", "cells" }, { "action": "add_sheet", "sheet" }, { "action": "merge" | "set_column_width" | "freeze" | "add_chart", … }] }
{ "type": "pptx", "open": "deck.pptx", "edits": [{ "action": "replace_text", "find", "replace" }, { "action": "append_slides", "slides": [] }] }
{ "type": "pdf", "open": "form.pdf", "edits": [{ "action": "fill_form", "fields": { "name": "Ada", "agree": true } }] }سینڈ باکس
- اسکرپٹ JavaScript انجن (QuickJS) میں چلتا ہے جس میں کوئی I/O نہیں: نہ نیٹ ورک (fetch، XMLHttpRequest)، نہ فائلیں (require، import، fs)، نہ process، نہ ٹائمر۔
- وہ انجن اسی آپریٹنگ سسٹم سینڈ باکس میں چلتا ہے جس میں MR Office غیر قابلِ اعتماد کوڈ چلاتا ہے: کوئی نیٹ ورک نیم اسپیس نہیں، صرف پڑھنے والا فائل سسٹم، کوئی نئے پروسیس نہیں، محدود CPU وقت، میموری اور کھلی فائلیں۔
- اسکرپٹ اپنی وقت کی حد پر (options.time_limit_seconds، طے شدہ 10 سیکنڈ)، 192 MB میموری پر اور اسٹیک ختم کرنے والی تکرار پر رک جاتا ہے، اور embed_builder_script_error کے ساتھ بتاتا ہے کہ کون سی۔
- جن تصویروں کو اسکرپٹ https URL سے نام دیتا ہے وہ سرور ہر ان پٹ کی طرح اسی محافظت شدہ fetch سے پڑھتا ہے، اسکرپٹ کبھی نہیں۔
حدود
اسکرپٹ Free پلان پر زیادہ سے زیادہ 10 سیکنڈ، Starter پر 30 اور Pro اور Enterprise پر 60 چلتا ہے؛ زیادہ سے زیادہ 512 KB اسکرپٹ، 5 ان پٹس اور 10 آؤٹ پٹ فائلیں۔ GET /api/v1/embed/builder (عوامی) بتاتا ہے کہ سرور کیا بناتا ہے: آیا اسکرپٹس وہاں چلتے ہیں، فارمیٹس اور حدود۔