问题描述
async createPDF(){
const page1 = PDFPage
.create()
.setMediaBox(200,200)
.drawText('You can add text and rectangles to the PDF!',{
x: 5,y: 235,color: '#007386',})
.drawRectangle({
x: 25,y: 25,width: 150,height: 150,color: '#FF99CC',})
.drawRectangle({
x: 75,y: 75,width: 50,height: 50,color: '#99FFCC',});
const jpgPath = 'storage/emulated/0/Download/image1.JPG';
const page2 = PDFPage
.create()
.setMediaBox(250,250)
.drawText('You can add JPG images too!')
.drawImage(jpgPath,'jpg',y: 125,width: 200,height: 100,})
const docsDir = await PDFLib.getDocumentsDirectory();
const pdfPath = `${docsDir}/MyPDF.pdf`;
PDFDocument
.create(pdfPath)
.addPages(page1)
.write() // Returns a promise that resolves with the PDF's path
.then(path => {
console.log('PDF created at: ' + path);
// Do stuff with your shiny new PDF!
});
}
因此,我正在使用上述PDF生成库(https://github.com/Hopding/react-native-pdf-lib)中的代码,并在到达确认消息步骤时成功创建了我的PDF。但是,当我导航到模拟器中的文件目录时,无论如何都找不到任何文件。这是源代码中包含PDF生成方法的部分。
/* @flow */
import PDFLib from './PDFLib';
import PDFPage from './PDFPage';
import type { PageAction } from './PDFPage';
export type DocumentAction = {
path: string,pages: PageAction[],modifyPages?: PageAction[],};
/**
* Here is some docs...
*/
export default class PDFDocument {
document: DocumentAction = {
path: '',pages: [],};
/**
* Create a new PDFDocument that will be written at the specified path
* @param {string} path - The absolute file path for this document to be
* written to.
*/
static create = (path: string) => {
const pdfDocument = new PDFDocument();
pdfDocument.setPath(path);
return pdfDocument;
}
static modify = (path: string) => {
const pdfDocument = new PDFDocument();
pdfDocument.setPath(path);
pdfDocument.document.modifyPages = [];
return pdfDocument;
}
setPath = (path: string) => {
this.document.path = path;
return this;
}
modifyPage = ({ page }: PDFPage) => {
if (page.pageIndex === undefined) {
throw new Error(
'Pages created with Page.create() must be added to document with ' +
'PDFDocument.addPage(),instead of PDFDocument.modifyPage()'
);
}
if (this.document.modifyPages === undefined) {
throw new Error(
'Cannot modify pages on PDFDocument initialized with PDFDocument.create(),' +
' please use PDFDocument.modify()'
)
}
this.document.modifyPages.push(page);
return this;
}
modifyPages = (...pages: PDFPage[]) => {
pages.forEach(page => {
this.modifyPage(page);
})
return this;
}
addPage = ({ page }: PDFPage) => {
if (page.pageIndex !== undefined) {
throw new Error(
'Pages created with Page.modify() must be added to document with ' +
'PDFDocument.modifyPage(),instead of PDFDocument.addPage()'
);
}
this.document.pages.push(page);
return this;
};
addPages = (...pages: PDFPage[]) => {
pages.forEach(page => {
this.addPage(page);
});
return this;
}
write = () => {
// console.log('Creating this PDFDocument:');
// console.log(this.document);
if (!this.document.path) {
return Promise.reject('PDFDocument must have a path specified!');
}
if (this.document.modifyPages !== undefined) {
return PDFLib.modifyPDF(this.document);
}
if (this.document.pages.length < 1) {
return Promise.reject('PDFDocument must have at least one page!');
}
return PDFLib.createPDF(this.document);
}
}
/* @flow */
export type TextAction = {
type: 'text',x: number,y: number,color: string,fontSize: number,fontName: string,value: string,};
export type RectangleAction = {
type: 'rectangle',width: number,height: number,};
export type ImageAction = {
type: 'image',imagePath: string,imageType: string,imageSource: string,width?: number,// If don't have width & height,will use actual dimensions
height?: number,};
export type PageActions =
TextAction
| RectangleAction
| ImageAction
;
export type PageAction = {
pageIndex?: number,// Not allowed in created pages
mediaBox?: { // Not allowed for modified pages
x: number,},actions: PageActions[],};
export default class PDFPage {
page: PageAction = {
actions: [],};
static create = () => {
const newPage = new PDFPage();
newPage.page.mediaBox = { x: 0,y: 0,width: 250,height: 500 };
return newPage;
}
static modify = (pageIndex) => {
const newPage = new PDFPage();
newPage.page.pageIndex = pageIndex;
return newPage;
}
setMediaBox = (
width: number,options: { x?: number,y?: number }={},) => {
if (this.page.pageIndex !== undefined) {
throw new Error('Cannot set media box on modified page!');
}
this.page.mediaBox = {
x: 0,...options,width,height,};
return this;
}
drawText = (
value: string,options: {
x?: number,y?: number,color?: string,fontSize?: number,}={}
) => {
const textAction: TextAction = {
x: 0,color: '#000000',fontSize: 12,fontName: 'Times-New-Roman',type: 'text',value,};
this.page.actions.push(textAction);
return this;
}
drawRectangle = (
options: {
x?: number,height?: number,}={}
) => {
const rectAction: RectangleAction = {
x: 0,type: 'rectangle',};
this.page.actions.push(rectAction);
return this;
}
drawImage = (
imagePath: string,options: {
x?: number,imageSource?: string
}={}
) => {
// TODO: Add logic using ReactNative.Image to automatically preserve image
// dimensions!
if (!['png','jpg'].includes(imageType)) {
throw new Error('Only JPG and PNG images are currently supported!');
}
if (typeof options.imageSource !== 'undefined' && !['assets','path'].includes(options.imageSource)) {
throw new Error('Only images from "assets" and "path" are currently supported!');
}
const imageAction: ImageAction = {
x: 0,source: 'path',type: 'image',imagePath,imageType
};
this.page.actions.push(imageAction);
return this;
}
}
为什么我看不到文件的任何想法?我需要安装扩展程序才能在模拟器上查看PDF或其他内容吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)