Facade API | Has Paid Plan | Univer Server | Univer on Node.js | Preset |
---|---|---|---|---|
✅ | ✅ | - | - | UniverSheetsAdvancedPreset |
Univer provides high-precision printing functions, supporting print preview, print settings, and export to PDF.
This feature includes the following plugin packages:
@univerjs-pro/sheets-print
Print Plugin
Presets Installation
import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsAdvancedPreset } from '@univerjs/presets/preset-sheets-advanced';
import UniverPresetSheetsAdvancedEnUS from '@univerjs/presets/preset-sheets-advanced/locales/en-US';
import { UniverSheetsDrawingPreset } from '@univerjs/presets/preset-sheets-drawing'
import UniverPresetSheetsDrawingEnUS from '@univerjs/presets/preset-sheets-drawing/locales/en-US'
import '@univerjs/presets/lib/styles/preset-sheets-core.css'
import '@univerjs/presets/lib/styles/preset-sheets-drawing.css'
import '@univerjs/presets/lib/styles/preset-sheets-advanced.css'
const { univerAPI } = createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsDrawingEnUS,
UniverPresetSheetsAdvancedEnUS,
),
},
theme: defaultTheme,
presets: [
UniverSheetsCorePreset(),
UniverSheetsDrawingPreset(),
UniverSheetsAdvancedPreset(),
],
});
Piecemeal Installation
npm install @univerjs-pro/sheets-print
import { LocaleType, merge, Univer } from '@univerjs/core';
import { defaultTheme } from "@univerjs/design";
import { UniverSheetsPrintPlugin } from '@univerjs-pro/sheets-print'
import SheetsPrintPluginEnUS from '@univerjs-pro/sheets-print/locale/en-US';
import '@univerjs-pro/sheets-print/facade';
import '@univerjs-pro/sheets-print/lib/index.css';
const univer = new Univer({
theme: defaultTheme,
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
SheetsPrintPluginEnUS
),
},
});
univer.registerPlugin(UniverSheetsPrintPlugin);
Facade API
To get full defination of facade api, please refer to FacadeAPI
Open Print Configuration Dialog
FWorkbook.openPrintDialog
method can open the print configuration dialog.
const workbook = univerAPI.getActiveWorkbook();
univerAPI.getHooks().onRendered(() => {
workbook.openPrintDialog();
})
Close Print Configuration Dialog
FWorkbook.closePrintDialog
method can close the print configuration dialog.
workbook.closePrintDialog();
Update Print Layout Config
Pass in the ISheetPrintLayoutConfig
type parameter, use FWorkbook.updatePrintConfig
method to update the print layout configuration.
workbook.updatePrintConfig({
area: 'CurrentSelection', // Print area
paperSize: 'A4', // Paper size
direction: 'Portrait', // Print direction
scale: 'Origin', // Scale
margin: 'Wide', // Margin
// ... Other configurations
});
Update Print Render Config
Pass in the ISheetPrintRenderConfig
type parameter, use FWorkbook.updatePrintRenderConfig
method to update the print render configuration.
workbook.updatePrintRenderConfig({
gridlines: true, // Show gridlines
hAlign: 'Center', // Horizontal alignment
vAlign: 'Center', // Vertical alignment
headerFooter: ['PageSize', 'WorkbookTitle', 'WorksheetTitle', 'Date', 'Time'], // Header and footer
// ... Other configurations
});
Call Up Print
FWorkbook.print
method can directly call up the print dialog.
// Using the default configuration you can pass in an empty object
workbook.updatePrintConfig({
// ... Print layout configuration
})
workbook.updatePrintRenderConfig({
// ... Print render configuration
})
workbook.print();
Save Screenshot to Clipboard
FWorkbook.saveScreenshotToClipboard
method can save the print data image to the clipboard.
This API is only available with a license. Without a license, usage is restricted, and save operations will return false
.
We use the Clipboard API to save the image to the clipboard, which may fail in an insecure network environment or in some unsupported browsers. A successful save will return true
.
const result = await workbook.saveScreenshotToClipboard();
console.log(result);
Get Range Screenshot
FRange.getScreenshot
method can get the range print data image.
This API is only available with a license. Users without a license will face usage restrictions. On failure, it returns false
, and on success, it returns the image’s base64 string.
const sheet = univerAPI.getActiveWorkbook().getActiveSheet();
const range = sheet.getRange('A1:G5');
console.log(range.getScreenshot());