我有同样的问题,我更改了pdf创建器,因为html创建了可变大小的pdf,并且它取决于分辨率设备,我使用pdf-lib可以在javascript中使用,可以创建或修改pdf,我写的很小以Expo为例,我打算创建一个图书馆
另外,您可以fill PDF
注意:它类似于react-native-pdf-lib,但只能在环境javascript中使用
我的App.tsx示例:
import React from "react";
import { StyleSheet,View,Text,Button } from "react-native";
import { Asset } from "expo-asset";
import * as Print from "expo-print";
import { degrees,PDFDocument,rgb,StandardFonts } from "pdf-lib";
export default function App({ context }: any) {
return (
<View style={styles.container}>
<Button
title="Generate PDF"
onPress={async () => {
const req = new XMLHttpRequest();
/* const templateUri = Asset.fromModule(
require("./assets/template.pdf")
);
console.log(templateUri); */
const url = 'https://pdf-lib.js.org/assets/with_update_sections.pdf' // templateUri.uri
req.open("GET",url,true);
req.responseType = "blob";
/* req.onprogress = e => (t.progress = e.loaded); */
req.onload = () => {
const blob = req.response;
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = async function() {
const base64data = reader.result as string; // template pdf in base64
const pdfDoc = await PDFDocument.load(base64data);
const helveticaFont = await pdfDoc.embedFont(
StandardFonts.Helvetica
);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const { width,height } = firstPage.getSize();
console.log(width,height);
firstPage.drawText('This text was added with JavaScript!',{
x: 5,y: height / 2 + 300,size: 50,font: helveticaFont,color: rgb(0.95,0.1,0.1),rotate: degrees(-45),});
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
Print.printAsync({ uri: pdfDataUri });
};
};
req.onerror = console.error;
req.send();
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,backgroundColor: "#fff",alignItems: "center",justifyContent: "center"
}
});
,
您可以尝试使用base64
字符串并将其解码回pdf文件。我认为它的尺寸应该较小,请继续。
- 使用Expo Print API获取有问题的pdf文件,请记住将base64设置为true。
import * as Print from 'expo-print';
const pdf = Print.printToFileAsync({
width: 612,// <=== your width
height: 612,// <=== your height
base64: true
});
- 您可以使用this或this库来解码回base64文件。
import * as Print from 'expo-print';
import base64 from 'base64'; // or import base64 from 'react-native-base64'
const pdf = Print.printToFileAsync({
width: 612,// <=== your width
height: 612,// <=== your height
base64: true
});
const pdfDecoded = base64(pdf);
pdfDecoded
将保存您的新pdf文件,并且文件应较小,您可以将其发送到服务器或显示它。
我还没有测试这2美分。
希望这会有所帮助!
,
如果您需要拍摄数码照片并将其添加到PDF文档中,并且对PDF文件的大小有限制,则在开始尝试缩小页面大小以缩小尺寸之前,该文档...。我强烈建议您考虑优化创建的PDF。
并非所有PDF软件都是平等创建的,许多创建者出生的文档that肿且可以从优化中受益匪浅。
请注意,1.9MB有点随意,尤其是在处理数字图像时,您可能还需要根据输入图像的质量和数量与利益相关者重新考虑这一要求。
我的公司提供了一个名为PDF Optimizer的工具,可以提供帮助。
本文链接:https://www.f2er.com/3151716.html