Convert Figma logo to code with AI

gjTool logopdfh5

web/h5/移动端PDF预览插件

1,020
244
1,020
76

Top Related Projects

50,177

PDF Reader in JavaScript

11,895

Client/server side PDF printing in pure JavaScript

10,139

A JavaScript PDF generation library for Node and the browser

29,627

Client-side JavaScript PDF generation for everyone.

Quick Overview

The pdfh5 project is a Python library that provides a simple and efficient way to extract text and images from PDF files and convert them into HTML5 format. It is designed to be a lightweight and easy-to-use tool for developers who need to work with PDF data in their applications.

Pros

  • Cross-platform compatibility: The library is written in Python, which makes it compatible with a wide range of operating systems, including Windows, macOS, and Linux.
  • Efficient PDF processing: The library uses a fast and reliable PDF parsing library, which allows it to process PDF files quickly and accurately.
  • Customizable output: The library allows users to customize the output HTML5 format, including the ability to specify the layout, styling, and metadata.
  • Easy integration: The library is easy to integrate into existing Python-based projects, with a simple and intuitive API.

Cons

  • Limited PDF feature support: The library may not support all the features and functionality of PDF files, such as complex layouts, annotations, or interactive elements.
  • Dependency on external libraries: The library relies on several external libraries, which may increase the complexity of the installation and setup process.
  • Potential performance issues: Depending on the size and complexity of the PDF files being processed, the library may experience performance issues, especially when processing large or complex documents.
  • Limited documentation: The project's documentation may be limited, which could make it more difficult for new users to get started with the library.

Code Examples

Here are a few examples of how to use the pdfh5 library:

from pdfh5 import PDF2HTML

# Convert a PDF file to HTML5
pdf = PDF2HTML('input.pdf')
html = pdf.convert()
with open('output.html', 'w') as f:
    f.write(html)

This code converts the input.pdf file to an HTML5 format and saves the result to the output.html file.

from pdfh5 import PDF2HTML

# Extract text from a PDF file
pdf = PDF2HTML('input.pdf')
text = pdf.get_text()
print(text)

This code extracts the text content from the input.pdf file and prints it to the console.

from pdfh5 import PDF2HTML

# Extract images from a PDF file
pdf = PDF2HTML('input.pdf')
images = pdf.get_images()
for i, image in enumerate(images):
    image.save(f'image_{i}.png')

This code extracts all the images from the input.pdf file and saves them as individual PNG files.

Getting Started

To get started with the pdfh5 library, follow these steps:

  1. Install the library using pip:
pip install pdfh5
  1. Import the PDF2HTML class from the pdfh5 module:
from pdfh5 import PDF2HTML
  1. Create a PDF2HTML object and use its methods to process a PDF file:
# Convert a PDF file to HTML5
pdf = PDF2HTML('input.pdf')
html = pdf.convert()
with open('output.html', 'w') as f:
    f.write(html)
  1. Customize the output by passing additional arguments to the convert() method:
# Customize the output HTML5
pdf = PDF2HTML('input.pdf')
html = pdf.convert(layout='grid', metadata={'title': 'My PDF Document'})
with open('output.html', 'w') as f:
    f.write(html)
  1. Explore the other methods provided by the PDF2HTML class, such as get_text() and get_images(), to extract specific content from the PDF file.

For more detailed information and advanced usage, please refer to the project's documentation.

Competitor Comparisons

50,177

PDF Reader in JavaScript

Pros of pdf.js

  • More comprehensive and feature-rich PDF rendering solution
  • Widely adopted and battle-tested in production environments
  • Extensive documentation and community support

Cons of pdf.js

  • Larger file size and potentially higher resource usage
  • Steeper learning curve for implementation and customization
  • May be overkill for simple PDF viewing needs

Code Comparison

pdf.js:

pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport({ scale: scale });
    // Render page
  });
});

pdfh5:

new Pdfh5('#demo', {
  pdfurl: 'path/to/document.pdf',
  zoomEnable: true,
  scrollEnable: true
});

Summary

pdf.js offers a more robust and feature-complete solution for PDF rendering, with extensive community support and documentation. However, it may be more complex to implement and have a larger footprint. pdfh5 provides a simpler, more lightweight option for basic PDF viewing needs, but with fewer advanced features and less widespread adoption.

11,895

Client/server side PDF printing in pure JavaScript

Pros of pdfmake

  • Client-side PDF generation in pure JavaScript, allowing for dynamic PDF creation in browsers
  • Extensive documentation and examples, making it easier for developers to get started
  • Supports complex layouts, tables, and styling options for creating professional-looking documents

Cons of pdfmake

  • Larger file size compared to pdfh5, which may impact load times for web applications
  • Steeper learning curve due to its more comprehensive feature set
  • Limited support for existing PDF manipulation (primarily focused on PDF creation)

Code Comparison

pdfmake:

var docDefinition = {
  content: [
    'Hello world',
    { text: 'Styled text', style: 'header' },
    { table: { body: [['Column 1', 'Column 2'], ['Value 1', 'Value 2']] } }
  ]
};
pdfMake.createPdf(docDefinition).download();

pdfh5:

var pdfh5 = new Pdfh5('#demo', {
  pdfurl: './path/to/your.pdf'
});
pdfh5.renderPdf();

The code comparison shows that pdfmake is focused on creating PDFs from scratch using JavaScript objects, while pdfh5 is primarily used for rendering existing PDFs in the browser.

10,139

A JavaScript PDF generation library for Node and the browser

Pros of PDFKit

  • More comprehensive PDF creation capabilities, allowing for complex document generation
  • Better documentation and wider community support
  • Supports both server-side (Node.js) and client-side (browser) usage

Cons of PDFKit

  • Larger file size and potentially higher resource usage
  • Steeper learning curve for beginners due to more advanced features
  • Primarily focused on PDF creation rather than viewing

Code Comparison

PDFKit (PDF creation):

const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Hello, world!');
doc.end();

PDFH5 (PDF viewing):

var pdfh5 = new Pdfh5('#demo', {
  pdfurl: './test.pdf'
});
pdfh5.renderPdf(1);

Key Differences

  • PDFKit is primarily for creating PDFs, while PDFH5 focuses on viewing PDFs in browsers
  • PDFKit offers more control over PDF content and structure
  • PDFH5 provides a simpler API for displaying PDFs in web applications
  • PDFKit has broader platform support, including server-side usage
  • PDFH5 is more lightweight and easier to integrate for basic PDF viewing needs
29,627

Client-side JavaScript PDF generation for everyone.

Pros of jsPDF

  • More comprehensive PDF creation capabilities, including text, images, and vector graphics
  • Wider browser compatibility and no external dependencies
  • Larger community and more frequent updates

Cons of jsPDF

  • Steeper learning curve due to more complex API
  • Larger file size, which may impact page load times
  • Limited built-in support for viewing PDFs in the browser

Code Comparison

pdfh5:

var pdfh5 = new Pdfh5('#demo', {
  pdfurl: './path/to/pdf'
});

jsPDF:

var doc = new jsPDF();
doc.text('Hello world!', 10, 10);
doc.save('a4.pdf');

Summary

pdfh5 is primarily focused on PDF viewing within the browser, offering a simpler API for basic PDF display. jsPDF, on the other hand, provides a more powerful toolkit for creating PDFs from scratch, with extensive options for content manipulation. While pdfh5 excels in quick PDF viewing integration, jsPDF is better suited for applications requiring dynamic PDF generation and complex document creation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

pdfh5.js

npm version npm downloads npm downloads MIT GitHub issues GitHub stars GitHub forks

1、有问题可以加Q群咨询,技术交流群,也可以探讨技术,另有微信群可以问群主拉入微信群

2、如果有报错,请复制example运行,然后对照相关文件,以及package.json,缺什么补什么

3、如果有某些字体显示不了,那可能是pdf.js缺少相关字库解析,可以尝试更改cMapUrl,建议去官方地址找版本

4、如果IOS下pdf显示不了,安卓却可以,可能是pdf精度过高导致,Safari浏览器canvas渲染绘制图片宽高不能超过16777216,超过会不显示

react、vue均可使用

example/test是vue使用示例

example/vue3demo是vue3使用示例

example/vite4vue3是vite4+vue3+ts使用示例

example/react-test是react使用示例

pdfh5.js示例

更新信息

  • 2024.02.29 更新: 修复部分bugs。

pdfh5在线预览 (建议使用谷歌浏览器F12手机模式打开预览)

快速使用(有两种方式)

一、script标签引入方式(需下载本项目文件夹css/pdfh5.css、js内所有文件)

  • 1.引入css
<link rel="stylesheet" href="css/pdfh5.css" />
  • 2.创建div
<div id="demo"></div>
  • 3.依次引入js(需引用本项目的js,不要引用官方的pdf.js,jquery可以引用其它版的)
<script src="js/pdf.js" type="text/javascript" charset="utf-8"></script>
<script src="js/pdf.worker.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/pdfh5.js" type="text/javascript" charset="utf-8"></script>
  • 4.实例化
var pdfh5 = new Pdfh5('#demo', {
  pdfurl: "./default.pdf"
});

二、npm安装方式(适应于vue), react使用方法类似vue(example/react-test是react使用示例)

  • 1.安装
npm install pdfh5
  • 2.使用
<template>
  <div id="app">
	<div id="demo"></div>
  </div>
</template>
<script>
  import Pdfh5 from "pdfh5";
  export default {
    name: 'App',
	data() {
	  return {
	    pdfh5: null
	  };
	},
	mounted() {
		//实例化
	  this.pdfh5 = new Pdfh5("#demo", {
		pdfurl: "../../static/test.pdf",
		// cMapUrl:"https://unpkg.com/pdfjs-dist@3.8.162/cmaps/",
		// responseType: "blob" // blob arraybuffer
	  });
	  //监听完成事件
	  this.pdfh5.on("complete", function (status, msg, time) {
		console.log("状态:" + status + ",信息:" + msg + ",耗时:" + time + "毫秒,总页数:" + this.totalNum)
		//禁止手势缩放
		this.pdfh5.zoomEnable(false);
	  })
	}
  }
</script>

<style>
	@import "pdfh5/css/pdfh5.css";
	*{
	padding: 0;
	margin: 0;
	}
	html,body,#app {
	width: 100%;
	height: 100%;
	}
</style>
  • 注意:如果css引用报错的话,按下面的方式引用。
  import Pdfh5 from "pdfh5";
  import "pdfh5/css/pdfh5.css";

API接口方法

实例化

  • pdfh5实例化的时候传两个参数,selector选择器,options配置项参数,会返回一个pdfh5实例对象,可以用来操作pdf,监听相关事件
var pdfh5 = new Pdfh5(selector, options);
参数名称类型取值是否必须作用
selectorString-√pdfh5的容器选择器
optionsObject-×pdfh5的配置项参数

options配置项参数列表

  • 示例: 配置项参数 pdfurl
var pdfh5 = new Pdfh5('#demo', {
	pdfurl: "./default.pdf"
});
参数名称类型取值作用
pdfurlString-pdf地址
responseTypeStringblob 、 arraybuffer 默认 blob请求pdf数据格式
URIenableBooleantrue、false, 默认falsetrue开启地址栏file参数
dataArray(arraybuffer)-pdf文件流 ,与pdfurl二选一(二进制PDF数据。使用类型化数组(Uint8Array)可以提高内存使用率。如果PDF数据是BASE64编码的,请先使用atob()将其转换为二进制字符串。)
renderTypeString"canvas"、"svg",默认"canvas"pdf渲染模式
pageNumBooleantrue、false, 默认true是否显示左上角页码
backTopBooleantrue、false, 默认true是否显示右下角回到顶部按钮
maxZoomNumber最大倍数3手势缩放最大倍数
scaleNumber最大比例5,默认1.5pdf渲染的比例
scrollEnableBooleantrue、false, 默认true是否允许pdf滚动
zoomEnableBooleantrue、false, 默认true是否允许pdf手势缩放
cMapUrlString"解析pdf时,特殊情况下显示完整字体的cmaps文件夹路径,例如 cMapUrl:"https://unpkg.com/pdfjs-dist@2.0.943/cmaps/"
limitNumber默认0限制pdf加载最大页数
logoObject{src:"pdfh5.png",x:10,y:10,width:40,height:40}src水印图片路径(建议使用png透明图片),width水印宽度,height水印高度,以每页pdf左上角为0点,x、y为偏移值。 默认false给每页pdf添加水印logo(canvas模式下使用)
gotoNumber默认0加载pdf跳转到第几页
textLayerBooleantrue、false, 默认false是否开启textLayer,可以复制文本(canvas模式下使用)【处于测试阶段,位置偏移严重】
backgroundObject{color:"#fff",image:"url('pdfh5.png')",repeat:"no-repeat",position:"left top",size:"40px 40px"},和css的background属性语法相同,默认false是否开启背景图模式

pdf请求示例

new Pdfh5('#demo', {
	pdfurl: "git.pdf",
	// responseType: "blob" // blob arraybuffer
});
  1. pdf文件流或者buffer已经得到,如何渲染
 new Pdfh5('#demo', {
 	data: blob,  //blob arraybuffer
 });

methods 方法列表

  • 示例: 是否允许pdf滚动
pdfh5.scrollEnable(true) //允许pdf滚动
pdfh5.scrollEnable(false) //不允许pdf滚动
方法名传参传参取值作用
scrollEnableBooleantrue、false, 默认true是否允许pdf滚动(需要在pdf加载完成后使用)
zoomEnableBooleantrue、false, 默认true是否允许pdf手势缩放(需要在pdf加载完成后使用)
showFunction带一个回调函数pdfh5显示
hideFunction带一个回调函数pdfh5隐藏
resetFunction带一个回调函数pdfh5还原
destroyFunction带一个回调函数pdfh5销毁
on(String, Function)String:监听的事件名,Function:监听的事件回调on方法监听所有事件
gotoNumberNumber:要跳转的pdf页数pdf跳转到第几页(pdf加载完成后使用)
download(String, Function)String:下载pdf的名称,默认download.pdf,Function:下载完成后的回调下载pdf

on方法监听所有事件-事件名列表

  • 示例: 监听pdf准备开始渲染,此时可以拿到pdf总页数
pdfh5.on("ready", function () {
	console.log("总页数:" + this.totalNum)
})
事件名回调作用
initFunction监听pdfh5开始初始化
readyFunction监听pdf准备开始渲染,此时可以拿到pdf总页数
errorFunction(msg,time)监听加载失败,msg信息,time耗时
successFunction(msg,time)监听pdf渲染成功,msg信息,time耗时
completeFunction(status, msg, time)监听pdf加载完成事件,加载失败、渲染成功都会触发。status有两种状态success和error
renderFunction(currentNum, time, currentPageDom)监听pdf渲染过程,currentPageDom当前加载的pdf的dom,currentNum当前加载的pdf页数,
zoomFunction(scale)监听pdf缩放,scale缩放比例
scrollFunction(scrollTop,currentNum)监听pdf滚动,scrollTop滚动条高度,currentNum当前页码
backTopFunction监听回到顶部按钮的点击事件回调
zoomEnableFunction(flag)监听允许缩放,flag:true,false
scrollEnableFunction(flag)监听允许滚动,flag:true,false
showFunction监听pdfh5显示
hideFunction监听pdfh5隐藏
resetFunction监听pdfh5还原
destroyFunction监听pdfh5销毁

打赏榜单

  • JayLin ï¿¥6.66
  • 靓仔城 ï¿¥6.67
  • 南蓝 ï¿¥8.80
  • 我是太阳 ï¿¥29.99
  • *小波 ï¿¥1.00
  • *鑫 Â¥9.99
  • *手 Â¥9.99
  • *勇 ï¿¥19.99
  • *爷 Â¥5.00
  • *超 Â¥20.00
  • 3*Y Â¥5.00
  • *阳 Â¥5.00
  • **雄 Â¥5.00
  • A*r Â¥1.23
  • *客 Â¥5.00
  • *运 Â¥66.66
  • *è¾° Â¥30.00
  • *黎 Â¥6.66+Â¥5.00
  • **福 Â¥6.66
  • *🏀 Â¥6.66+Â¥1.00
  • *阳 Â¥10.00
  • 自闭中 Â¥16.66+Â¥16.00
  • *焕 Â¥6.66
  • *人 Â¥5.00
  • *。 Â¥5.20
  • 半*) Â¥5.00
  • *1 Â¥15.00
  • *蕾 Â¥16.66+Â¥8.80
  • *军 Â¥10.00
  • **强 Â¥58.88
  • E*y Â¥6.60
  • J*u Â¥13.00
  • A*a Â¥50.00
  • *东 Â¥8.80
  • j*y Â¥9.99
  • *宇 Â¥6.66
  • *涛 ï¿¥1.00
  • *. ï¿¥10.00
  • *☺ ï¿¥6.66
  • *霸 ï¿¥6.66
  • a*r ï¿¥20.00
  • 木槿(**耀) ï¿¥50.00