import { common } from '@kit.AbilityKit'; import fs, { ReadOptions, WriteOptions } from '@ohos.file.fs'; import { BusinessError, print } from '@kit.BasicServicesKit'; import { promptAction } from '@kit.ArkUI'; @Entry @Component struct Index { @State message: string = '开始写入PDF' @State isOpenPdfButtonShow: Visibility = Visibility.Hidden @State writePdfProgress: number = 0 private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext private pathDir: string = '/data/storage/el2/base/haps/entry/files' private filePath: string = this.pathDir + "/test.pdf"; private fileFd: number = 0 async startPrint() { let jobName: string = 'test' const filePath: string = this.context.filesDir + "/testShare1.pdf"; let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); let printAdapter: print.PrintDocumentAdapter = { onStartLayoutWrite(jobId: string, oldAttrs: print.PrintAttributes, newAttrs: print.PrintAttributes, fd: number, writeResultCallback: (jobId: string, writeResult: print.PrintFileCreationState) => void) { console.log('printxxx', 'jobId:', jobId, 'oldAttrs:', JSON.stringify(oldAttrs), 'newAttrs:', JSON.stringify(newAttrs), 'fd:', fd.toString()); // 用openSync读取 let bufSize = 4096; let readSize = 0; let buf = new ArrayBuffer(bufSize); let readOptions: ReadOptions = { offset: readSize, length: bufSize }; let readLen = fs.readSync(file.fd, buf, readOptions); while (readLen > 0) { readSize += readLen; let writeOptions: WriteOptions = { length: readLen }; fs.writeSync(fd, buf, writeOptions); readOptions.offset = readSize; readLen = fs.readSync(file.fd, buf, readOptions); } writeResultCallback(jobId, print.PrintFileCreationState.PRINT_FILE_CREATED) }, onJobStateChanged(): void { } } let printAttributes: print.PrintAttributes = { copyNumber: 1, pageRange: { startPage: 0, endPage: 5, pages: [] }, pageSize: print.PrintPageType.PAGE_ISO_A3, directionMode: print.PrintDirectionMode.DIRECTION_MODE_AUTO, colorMode: print.PrintColorMode.COLOR_MODE_MONOCHROME, duplexMode: print.PrintDuplexMode.DUPLEX_MODE_NONE } print.print(jobName, printAdapter, printAttributes, this.context).then((printTask: print.PrintTask) => { printTask.on('succeed', () => { console.log('print state is succeed') }) }).catch((err: BusinessError) => { console.log('print err ' + JSON.stringify(err)) }) } createFile(): void { getContext(this).resourceManager.getRawFileContent('G.G 2017-1.pdf', (err: BusinessError, value: Uint8Array) => { const filePath: string = this.context.filesDir + "/testShare1.pdf"; // let file = fs.openSync(this.pathDir + '/testShare.pdf', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); let arrayBuffer = value.buffer.slice(0) fs.writeSync(file.fd, arrayBuffer) fs.closeSync(file); promptAction.showToast({ message: '沙箱pdf创建成果', duration: 2000 }); }) // getContext(this).resourceManager.getRawFileContent('startIcon.png', (err: BusinessError, value: Uint8Array) => { // let file = fs.openSync(this.pathDir + '/printImage.png', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); // let arrayBuffer = value.buffer.slice(0) // fs.writeSync(file.fd, arrayBuffer) // fs.closeSync(file); // }) } build() { Column() { Button('创建PDF文件') .onClick(() => { this.createFile() }) Button('打印') .onClick(() => { this.startPrint() }) } .width('100%') .width('100%') .justifyContent(FlexAlign.Center) } }在rowfile目录下新增G.G 2017-1.pdf文件"requestPermissions": [ { "name": "ohos.permission.PRINT", }, { "name": "ohos.permission.INTERNET" } ]
在rowfile目录下新增G.G 2017-1.pdf文件